servlet用户管理项目

来源:互联网 发布:淘宝儿童玩具 编辑:程序博客网 时间:2024/06/06 02:40

LoginServlet.java

package com.sevend.view;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;@SuppressWarnings("serial")public class LoginServlet extends HttpServlet {/** * 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 {response.setContentType("text/html;charset=utf-8");//告诉浏览器以utf-8去解析(浏览器的默认编码就会成utf-8)response.setCharacterEncoding("utf-8");//设置编码方式两者都是同一个作用,配合使用更好PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println("<HEAD><TITLE>用户登陆界面</TITLE></HEAD>");out.println("<BODY>");out.println("<h1>用户登陆</h1>");//action路径规范:/web应用名/servlet的urlout.println("<form action='/UserManage/servlet/LoginCLServlet' method='post'>");out.println("用户名:<input type='text' name='username' id='username' /><br/>");out.println("密     码:<input type='password' name='password' id='password' /><br/>");out.println("<input type='submit' value='登陆' /><br/>");out.println("</form>");out.println("</BODY>");out.println("</HTML>");out.flush();out.close();}/** * 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);}}


LoginCLServlet.java

package com.sevend.controller;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;@SuppressWarnings("serial")public class LoginCLServlet extends HttpServlet {/** * 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 {response.setContentType("text/html;charset=utf-8");@SuppressWarnings("unused")PrintWriter out = response.getWriter();//接受用户提交的用户名和密码String username=request.getParameter("username");String password=request.getParameter("password");//验证一下时候能够接受数据//System.out.println("username="+username+"password="+password);if("sevend".equals(username)&&"123".equals(password)){//跳转到下一个页面,servlet提供了两种方法://一:sendredirect转向//sendredirect的url格式/web应用名称/servlet urlresponse.sendRedirect("/UserManage/servlet/MainFrame");//二:forward转发}else{response.sendRedirect("/UserManage/servlet/LoginServlet");}}/** * 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 {response.setContentType("text/html");@SuppressWarnings("unused")PrintWriter out = response.getWriter();this.doGet(request, response);}}


MainFrame.java

package com.sevend.view;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;@SuppressWarnings("serial")public class MainFrame extends HttpServlet {/** * 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 {response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");out.println("<HTML>");out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");out.println("  <BODY>");out.print("    This is ");out.print("MainFrame");out.println(", using the GET method");out.println("  </BODY>");out.println("</HTML>");out.flush();out.close();}/** * 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 {response.setContentType("text/html");@SuppressWarnings("unused")PrintWriter out = response.getWriter();this.doGet(request, response);}}