在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
0.新建web项目 1.设计login.jsp页面 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>登陆页面</title> </head> <body> <form action="login" method="post"> 用户名:<input name="username" type="text" /><br/> 密码:<input name="password" type="password" /><br/> <input type="submit" value="提交" /> </form> </body> </html> 2.新建servlet文件 双击,打开loginServlet.java文件,在doPost方法内,通过request.getParameter()方法获取login页面的username和password,并通过response.sendRedirect()方法跳转到index.jsp页面。页面代码如下: package a; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class loginServlet implements javax.servlet.Servlet{ public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ String userName = request.getParameter("username");//取得用户名 String password = request.getParameter("password");//取得密码 response.sendRedirect("index.jsp"); } public void destroy() { } public ServletConfig getServletConfig() { return null; } public String getServletInfo() { return null; } public void init(ServletConfig arg0) throws ServletException { } public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { HttpServletRequest rq = (HttpServletRequest)request; HttpServletResponse rs = (HttpServletResponse) response; doPost(rq,rs); } } 3.配置servlet <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>a.loginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app> 5.配置数据源 上图中的id作为主键,使得数据库的表至少符合第二范式的要求,其中username和password字段是login表内独有的字段,因此此表符合第三范式的要求。 (2)连接数据库 package a; import java.sql.*; public class DBUtil { boolean bInited = false; //加载驱动 public void initJDBC() throws ClassNotFoundException { //加载MYSQL JDBC驱动程序 Class.forName("com.mysql.jdbc.Driver"); bInited = true; System.out.println("Success loading Mysql Driver!"); } public Connection getConnection() throws ClassNotFoundException, SQLException{ if(!bInited){ initJDBC(); } //连接URL为 jdbc:mysql//服务器地址/数据库名 //后面的2个参数分别是登陆用户名和密码 Connection conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/数据库名","用户名","密码"); return conn; } public boolean loginSuccess(String userName,String password){ boolean returnValue = false; String sql = "SELECT * FROM login"; Connection conn = null; Statement stmt = null; ResultSet rs = null; try{ conn = getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while(rs.next()){ String userNameInDB = rs.getString("username"); String passwordInDB = rs.getString("password"); if(userNameInDB.equals(userName) && passwordInDB.equals(password)){ returnValue = true; break; } } }catch (ClassNotFoundException e) { e.printStackTrace(); }catch (SQLException e) { e.printStackTrace(); } return returnValue; } } 上文中loginSuccess()方法内,用于在数据库中查找用户名和密码与传入参数username、password匹配的情况。一旦找到,则返回true结果。 (3)修改Servlet业务逻辑 public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{ String userName = request.getParameter("username");//取得用户名 String password = request.getParameter("password");//取得密码 DBUtil db = new DBUtil();//构建数据库对象 boolean canLogin = db.loginSuccess(userName, password); if(canLogin){//根据登陆情况,跳转页面 response.sendRedirect("index.jsp"); }else{ response.sendRedirect("login.jsp"); } } (4)测试页面 |
请发表评论