1 结构
2 LoginTestServlet.java
package com.test; import java.io.IOException; import java.sql.Connection;import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class LoginTestServlet */ @WebServlet("/LoginTestServlet") public class LoginTestServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginTestServlet() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=gb2312"); request.setCharacterEncoding("gb2312"); String result = ""; String username = request.getParameter("username"); String psw = request.getParameter("password"); if(username == "" || username == null || username.length()>20){ try { result = "请输入用户名(不能超过20个字符)"; request.setAttribute("message", result); response.sendRedirect("login.jsp"); return; } catch (Exception e) { e.printStackTrace(); } } if(psw == "" || psw == null || psw.length() > 20){ try { result = "请输入密码(不能超过20个字符)"; request.setAttribute("message", result); response.sendRedirect("login.jsp"); return; } catch (Exception e) { e.printStackTrace(); } } //登记JDBC驱动程序 try { Class.forName("org.postgresql.Driver"); } catch (Exception e) { System.out.print("Class Not Found Exception"); } //链接URL String url = "jdbc:postgresql://localhost:5432/mydb"; Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = (Connection) DriverManager.getConnection(url,"postgres","postgres"); stmt = (Statement) conn.createStatement(); String sql = "select * from userInfo where username='"+username+"' and userpsw= '"+psw+"'"; rs = stmt.executeQuery(sql); } catch (Exception e) { e.printStackTrace(); } HttpSession session = request.getSession(); session.setAttribute("username", username); try { if(rs.next()){ session.setAttribute("age",rs.getString("age")) ; session.setAttribute("sex",rs.getString("sex")) ; session.setAttribute("weight",rs.getString("weight")) ; response.sendRedirect("success.jsp") ; return; }else{ session.setAttribute("message", "用户名或密码不匹配。"); response.sendRedirect("fail.jsp") ; return; } } catch (Exception e) { e.printStackTrace(); } } }
3 login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>User Login
4 success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here <% String userName = (String)session.getAttribute("username") ; String age = (String)session.getAttribute("age") ; String weight = (String)session.getAttribute("weight") ; String sex = (String)session.getAttribute("sex") ; System.out.println("性别:A" + sex +"A") ; /* if (sex.trim().equals("M")) { sex ="男" ; }else{ sex = "女" ; } */ %><%=userName %> 欢迎您,登陆成功!
登陆用户信息:姓名: <%=userName %> 年龄: <%=age %> 体重: <%=weight %> kg 性别: <%=sex %> 返回
5 fail.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here <% String userName = (String) session.getAttribute("username"); String msg = (String) session.getAttribute("message"); %><%=userName%> 对不起,登陆失败!
原因: <%=msg%>
5秒后将返回登陆界面。 <% response.setHeader("Refresh", "5;URL=/ServletDemo01/login.jsp"); %>
6. 数据库表
CREATE TABLE userinfo( username character varying NOT NULL, userpsw character varying NOT NULL, age integer, sex character(1), weight integer, CONSTRAINT userinfo_pkey PRIMARY KEY (username))
7.web.xml
ServletDemo01 index.html index.htm index.jsp default.html default.htm default.jsp LoginTestServlet com.test.LoginTestServlet LoginTestServlet /loginTestServlet