效果图
登录页面代码:login.html
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>登录</title> 8 </head> 9 <body> 10 <fieldset> 11 <legend>用户登录</legend> <br /> 12 <!-- form 表单的action 属性值要和配置在web.xml文件中的servlet的url-pattern相同 --> 13 <form action="loginServlet" method="post" name="login"> 14 用户名:<input type="text" name="username" /> <br /> <br /> 15 密 码:<input type="password" name="password" /> <br /> <br /> 16 <input type="submit" value="登录" /> 17 </form> 18 </fieldset> 19 </body> 20 </html>
创建Servlet:LoginServlet.java ——(Servlet就是一个java文件,像创建类那样创建就行了,不过要继承HttpServlet类)
1 package com.myweb.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class LoginServlet extends HttpServlet { 12 private static final long serialVersionUID = 1L; 13 14 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 15 //一般都会在doGet()方法中调用doPost()方法,或者在doPost()方法中调用doGet()方法 16 doPost(request, response); 17 } 18 19 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 20 //设置编码格式,可以防止乱码出现 21 response.setContentType("text/html; charset=UTF-8"); 22 response.setCharacterEncoding("utf-8"); 23 request.setCharacterEncoding("utf-8"); 24 //获取用户提交过来的用户名和密码,是通过表单中input的name属性获取的 25 String username = request.getParameter("username"); 26 String password = request.getParameter("password"); 27 28 System.out.println(username+":"+password); 29 //获取输出流,用户显示信息,也可以用转发或者重定向 30 PrintWriter pw = response.getWriter(); 31 if("admin".equals(username) && "123".equals(password)){ 32 pw.write("login success"); 33 /* 34 * 要先创建success.jsp文件 35 */ 36 //转发 37 // request.getRequestDispatcher("success.jsp").forward(request, response);; 38 //重定向 39 // response.sendRedirect("success.jsp"); 40 }else{ 41 pw.write("login fail"); 42 /* 43 * 要先创建fail.jsp文件 44 */ 45 //转发 46 // request.getRequestDispatcher("fail.jsp").forward(request, response);; 47 //重定向 48 // response.sendRedirect("fail.jsp"); 49 } 50 51 } 52 53 }
配置文件:web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 3 <display-name>myweb</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 <servlet> 13 <servlet-name>LoginServlet</servlet-name> 14 <!-- 全类名:包和类全部要写 --> 15 <servlet-class>com.myweb.servlet.LoginServlet</servlet-class> 16 </servlet> 17 <servlet-mapping> 18 <servlet-name>LoginServlet</servlet-name> 19 <url-pattern>/loginServlet</url-pattern> 20 </servlet-mapping> 21 </web-app>
创建工程是有些人的可能会没有 web.xm 文件,只要是在创建工程时在最后一步没有勾选,如图所示
所以要把这个选上。没有勾选上的可以自行在webContent--WEB-INF--lib 创建。