📄 adduserservlet.java
字号:
package com.liubin.servlet.user;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.liubin.model.*;
import com.liubin.service.user.*;
import java.util.*;
public class AddUserServlet extends HttpServlet {
/**
* Constructor of the object.
*/
public AddUserServlet() {
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 {
request.setCharacterEncoding("gbk");
String name=request.getParameter("name").trim();
String password=request.getParameter("password").trim();
String password2=request.getParameter("password2").trim();
String sex=request.getParameter("sex");
int department_id=Integer.parseInt(request.getParameter("department_id"));
String telephone =request.getParameter("telephone").trim();
String email=request.getParameter("email");
int role_id=Integer.parseInt(request.getParameter("role_id"));
int level_id=Integer.parseInt(request.getParameter("level_id"));
String address =request.getParameter("address").trim();
User user=null;
String message=null;
String path=null;
UserService service=new UserService();
if(name==null||name.length()<=0)
{
request.getRequestDispatcher("user/addUser.jsp").forward(request, response);
}
else if(password==null||password.length()<=0)
{
request.getRequestDispatcher("user/addUser.jsp").forward(request, response);
}
else if(telephone==null||telephone.length()<=0)
{
request.getRequestDispatcher("user/addUser.jsp").forward(request, response);
}
else if(address==null||address.length()<=0)
{
request.getRequestDispatcher("user/addUser.jsp").forward(request, response);
}
else if(password2==null||password2.length()<=0)
{
request.getRequestDispatcher("user/addUser.jsp").forward(request, response);
}
else
{
if(password.equals(password2)){
if(service.checkUser(name) ){
user=new User();
user.setId(UUID.randomUUID().toString().replaceAll("-", ""));
user.setName(name);
user.setAddress(address);
user.setAdmin_flag("N");
user.setLevel_id(level_id);
user.setRole_id(role_id);
user.setDepartment_id(department_id);
user.setPassword(password);
user.setEmail(email);
user.setSex(sex);
user.setTelephone(telephone);
if( service.inserUser(user)>=1)
{
path="/listUserServlet";
}
else
path="user/addUser.jsp";
}
else
{
path="user/addUser.jsp";
}
}
else{
message="输入的两次密码不争取";
path="user/addUser.jsp";
}
request.getRequestDispatcher(path).forward(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 {
this.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 + -