?? loginservlet.java
字號:
package com.ghy.bookstore.user.web;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.ghy.bookstore.service.BookStoreService;
import com.ghy.bookstore.service.BookStoreServiceFactory;
import com.ghy.bookstore.user.model.UserVO;
public class LoginServlet extends HttpServlet {
public LoginServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public String invalid(UserVO user) throws IOException, InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException
{
BookStoreService bsService=BookStoreServiceFactory.getBookStoreService();
String message="";
if(user.getName()==null||user.getName().trim().equals(""))
message="用戶名為空";
if(!message.equals(""))
return message;
if(user.getPassword()==null||user.getPassword().trim().equals(""))
message="密碼為空";
if(!message.equals(""))
return message;
if(!bsService.login(user))
message="用戶名或密碼錯誤";
if(!message.equals(""))
return message;
else
return "";
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
UserVO user=new UserVO();
user.setName(request.getParameter("username"));
user.setPassword(request.getParameter("userpassword"));
String message;
try {
message = this.invalid(user);
if(!message.equals(""))
{
request.getRequestDispatcher("/jsp/user/login.jsp?message="+URLEncoder.encode(message, "utf-8")).forward(request, response);
return ;
}
HttpSession session=request.getSession();
BookStoreService bsService=BookStoreServiceFactory.getBookStoreService();
session.setAttribute("CurrentUser", bsService.findUserByName(user.getName()));
response.sendRedirect(request.getContextPath()+"/book/bookmain");
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -