📄 userloginservlet.java
字号:
package com.test.web.action;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.test.model.UserBean;
import com.test.servers.UserOperation;
import com.test.util.ErrorManager;
import com.test.util.KeyManager;
/**
* 用来控制添加用户的servlet
* @author pwf
* @version 1.0
*/
public class UserLoginServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public UserLoginServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @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 doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* 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 {
request.setCharacterEncoding("gbk");
String userName = request.getParameter("userName");
String texta = request.getParameter("texta");
String textb = request.getParameter("textb");
UserBean user = new UserBean();
user.setUsername(userName);
user.setStr_a(texta);
user.setStr_b(textb);
if (this.validate(user)) {
try {
//经过存储这一步,如果正常,user应该已经包含id等信息
if( new UserOperation().saveUserAndFill(user) ){
//正确则跳转到显示页面
this.gotoUserInfoDisp(request, response, user);
}else{
//否则跳转到错误页面
this.gotoErrorPage(request, response, "服务器忙,请稍候重试");
}
} catch (Exception e) {
// 如果添加用户出错,则跳到出错页面
this.gotoErrorPage(request, response, e.getMessage());
}
} else {
// 如果信息填写不完整,则跳到出错页面
this.gotoErrorPage(request, response, "请完整填写页面信息");
}
}
//跳转到user信息的显示页面
private void gotoUserInfoDisp(HttpServletRequest request, HttpServletResponse response,UserBean user){
try {
request.setAttribute(KeyManager.USERLOGINSERVLET_USER, user);
RequestDispatcher rd = request.getRequestDispatcher("newUserInfo.jsp");
rd.forward(request, response);
} catch (Exception e) {
e.printStackTrace();
this.gotoErrorPage(request, response, "服务器繁忙或您要寻找的页面并不存在");
}
}
//去错误页面
private void gotoErrorPage(HttpServletRequest request,
HttpServletResponse response, String message) {
try {
ErrorManager.addErrorMessage(request.getSession(), message);
response.sendRedirect("userError.jsp");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("找不到错误页面");
}
}
// 检查填入用户的有效性
private boolean validate(UserBean user) {
boolean isOK = true;
if (user.getUsername() == null || user.getUsername().equals("")) {
isOK = false;
}
if (user.getStr_a() == null || user.getStr_a().equals("")) {
isOK = false;
}
if (user.getStr_b() == null || user.getStr_b().equals("")) {
isOK = false;
}
return isOK;
}
/**
* 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 + -