⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 userservlet.java

📁 J2EE实现mvc模式的事例
💻 JAVA
字号:
package user;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserServlet extends HttpServlet {
	
	/**
	 * Constructor of the object.
	 */
	public UserServlet() {
		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 {
		ServletContext ctxt=this.getServletContext();
		UserService service=(UserService)ctxt.getAttribute("service");
		
		if(service==null){
			service=new UserService();
			ctxt.setAttribute("service", service);
		}
		
		List users=service.getAllUsers();
		
		request.setAttribute("allusers", users);
		RequestDispatcher dis=request.getRequestDispatcher("viewAllUsers");
		dis.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 {
		String action=request.getParameter("action");
		String userName=request.getParameter("userName");
		String pwd=request.getParameter("pwd");
		
		User user=new User(userName,pwd);
		
		ServletContext ctxt=this.getServletContext();
		
		System.out.println("UserServlet username"+ctxt.getInitParameter("username"));
		System.out.println("UserServlet rate"+this.getInitParameter("rate"));
		UserService service=(UserService)ctxt.getAttribute("service");
		if(service==null){
			service=new UserService();
			ctxt.setAttribute("service", service);
		}
		
		if(action!=null&&action.equals("login")){
			boolean result=service.login(user);
			
			if(result){
				//response.sendRedirect("success?userName="+userName);
				
				//RequestDispatcher dis=request.getRequestDispatcher("success");
				ServletContext bbsCtxt=ctxt.getContext("/bbs");
				RequestDispatcher dis=bbsCtxt.getRequestDispatcher("/welcome");
				dis.forward(request,response);
			}else{
				ctxt.log("Login Fail  "+request.getRemoteAddr());
				response.sendRedirect("fail.html");
			}
		}
		
		if(action!=null&&action.equals("reg")){
			service.reg(user);
			RequestDispatcher dis=request.getRequestDispatcher("success");
			dis.forward(request,response);
		}
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
		//....
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -