JavaEE——JSP开发模式

model1开发模式

JavaEE——JSP开发模式_第1张图片

  • 工作流程:

①浏览器请求,JSP页面接收

②JSP根据请求和JavaBean进行交互

③JavaBean进行业务处理,JDBC操纵数据库

④JSP将请求结果返回浏览器页面

  • JSP的理解

  JSP是servlet的简化版本,jsp引擎会将jsp编译为servlet,再对servlet进行编译

  • 利用model1,设计一个简单的登陆页面

  新建Java web项目,项目结构如下:

JavaEE——JSP开发模式_第2张图片

  建立基于Mysql的JDBC类

 1 package com.ddpapa.test.jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 
 9 public class MysqlDBConn {
10     private Statement stmt;
11     private Connection conn;
12     ResultSet rs;
13     public MysqlDBConn(){
14         stmt = null;
15         try {
16             /*加载数据库驱动获取数据库连接*/
17             Class.forName("com.mysql.jdbc.Driver");
18             conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/datatest","root","123456");
19         } catch (Exception e) {
20             // TODO: handle exception
21             e.printStackTrace();
22         }
23         rs = null;
24     }
25     public Connection getConn() {
26         return this.conn;
27     }
28     public ResultSet executeQuery(String sql){
29         /*取得结果集*/
30         try {
31             stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
32             rs = stmt.executeQuery(sql);
33         } catch (Exception e) {
34             // TODO: handle exception
35             System.err.println("Data.executeQuery: " + e.getMessage());
36         }
37         return rs;
38     }
39     public void closeStmt()
40     {
41         /*关闭Stmt*/
42         try
43         {
44             stmt.close();
45         }catch(SQLException e){
46             System.err.println("Data.executeQuery: " + e.getMessage());
47         }
48     }
49     public void closeConn()
50     {
51         /*关闭数据库连接*/
52         try
53         {
54             conn.close();
55         }catch(SQLException e){
56             System.err.println("Data.executeQuery: " + e.getMessage());
57         }
58     }
59     
60 }
MysqlDBConn.java

  建立JavaBean

 1 package com.ddpapa.test.model.vo;
 2 
 3 public class UserTable {
 4     private Integer id;
 5     private String username;
 6     private String password;
 7     public Integer getId() {
 8         return id;
 9     }
10     public void setId(Integer id) {
11         this.id = id;
12     }
13     public String getUsername() {
14         return username;
15     }
16     public void setUsername(String username) {
17         this.username = username;
18     }
19     public String getPassword() {
20         return password;
21     }
22     public void setPassword(String password) {
23         this.password = password;
24     }
25     
26 }
UserTable.java

  新建login.jsp

 1 <%@ page language="java" pageEncoding="gb2312" %>
 2 <html>
 3 <head>
 4     <title>登陆界面title>
 5 head>
 6 <body>
 7 <form action="validate.jsp" method="get">
 8     <table>
 9         <tr>
10             <th colspan="2" align="center">登陆页面th>
11         tr>
12         <tr>
13             <td>用户名:td>
14             <td><input type="text" name="username" size="20">td>
15         tr>
16         <tr>
17             <td>密码:td>
18             <td><input type="password" name="password" size="20">td>
19         tr>
20     table>
21     <input type="submit" value="登陆">
22     <input type="reset" value="重置">
23 form>
24 body>
25 html>
login.jsp

  新建validate.jsp

 1 <%@page language="java" pageEncoding="gb2312" import="java.sql.*,com.ddpapa.test.model.vo.UserTable"%>
 2 <jsp:useBean id="MysqlDB" scope="page" class="com.ddpapa.test.jdbc.MysqlDBConn">jsp:useBean>
 3 <html>
 4 <head>
 5     <meta http-equiv="Content-Type" content="text/html;charset=gb2312">
 6 head>
 7 <body>
 8     <%
 9         request.setCharacterEncoding("gb2312");
10         String usr=request.getParameter("username");
11         String pwd=request.getParameter("password");
12         boolean validate = false;
13         UserTable user = null;
14         user = (UserTable)session.getAttribute("user");
15         if(user==null){
16             String sql = "select * from userTable";
17             ResultSet rs = MysqlDB.executeQuery(sql);    //取得结果集
18             while(rs.next()){
19                 if((rs.getString("username").trim().compareTo(usr)==0) && (rs.getString("password").compareTo(pwd)==0)){
20                     user = new UserTable();
21                     user.setId(rs.getInt(1));
22                     user.setUsername(rs.getString(2));
23                     user.setPassword(rs.getString(3));
24                     validate = true;
25                 }
26             }
27             rs.close();
28             MysqlDB.closeStmt();
29             MysqlDB.closeConn();
30         }else{
31             validate = true;
32         }
33         if(validate){
34         %>
35             <jsp:forward page="success.jsp"/>
36         <% 
37         }else{
38         %>
39             <jsp:forward page="error.jsp"/>
40         <%
41         }
42      %>
43 body>
44 html>
validate.jsp

  新建error.jsp

1 <%@ page language="java" pageEncoding="gb2312" %>
2 <html>
3 <head>
4     <title>登陆失败title>
5 head>
6 <body>
7 抱歉!登陆失败
8 body>
9 html>

  新建success.jsp

 1 <%@ page language="java" pageEncoding="gb2312" import="com.ddpapa.test.model.vo.UserTable"%>
 2 <html>
 3 <head>
 4     <title>登陆成功title>
 5 head>
 6 <body>
 7     <%
 8         String usr = request.getParameter("username");
 9      %>
10      <%=usr%>,您好!欢迎登陆。
11 body>
12 html>

   修改web.xml配置文件

1 xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
3   <display-name>jsp_model1display-name>
4   <welcome-file-list>
5     <welcome-file>login.jspwelcome-file>
6   welcome-file-list>
7 web-app>
  • 错误集锦

  500错误——lib目录下缺少数据库连接jar包

  JavaEE——JSP开发模式_第3张图片

  500错误——org.apache.jasper.JasperException:An exception occurred processing JSP page [/xxx.jsp] at line [x]

  success代码如下时出现错误:

JavaEE——JSP开发模式_第4张图片

   错误原因session对象getAttribute返回值为null(根本原因user不存在),导致空指针错误,改为request对象解决问题

JavaEE——JSP开发模式_第5张图片

model2开发模式

JavaEE——JSP开发模式_第6张图片

 

 

  •  工作流程

  ①浏览器请求,servlet接收到请求和JavaBean进行交互

  ②JavaBean进行业务处理,通过JDBC完成与数据库交互

  ③servlet将将请求结果给JSP

  ④JSP以页面形式展示给浏览器

  • 在model1的基础上利用model2设计登陆

  项目结构如下:

JavaEE——JSP开发模式_第7张图片

 

 

   新建MainServlet.java文件(选择新建servlet即可自动生成doGet和doPost方法),代码如下:

 1 package com.ddpapa.test.model.servlet;
 2 
 3 import java.io.IOException;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import javax.servlet.http.HttpSession;
12 
13 import com.ddpapa.test.jdbc.MysqlDBConn;
14 import com.ddpapa.test.model.vo.UserTable;
15 
16 public class MainServlet extends HttpServlet {
17     public MainServlet() {
18         super();
19     }
20     public void doGet(HttpServletRequest request, HttpServletResponse response)
21             throws ServletException, IOException {
22         request.setCharacterEncoding("gb2312");
23         String usr=request.getParameter("username");
24         String pwd=request.getParameter("password");
25         boolean validate = false;
26         HttpSession session = request.getSession();
27         UserTable user = null;
28         user = (UserTable)session.getAttribute("user");
29         MysqlDBConn MysqlDB = new MysqlDBConn();
30         if(user==null){
31             String sql = "select * from userTable";
32             ResultSet rs = MysqlDB.executeQuery(sql);    //取得结果集
33             try {
34                 while(rs.next()){
35                     if((rs.getString("username").trim().compareTo(usr)==0) && (rs.getString("password").compareTo(pwd)==0)){
36                         user = new UserTable();
37                         user.setId(rs.getInt(1));
38                         user.setUsername(rs.getString(2));
39                         user.setPassword(rs.getString(3));
40                         validate = true;
41                     }
42                 }
43             } catch (SQLException e) {
44                 // TODO Auto-generated catch block
45                 e.printStackTrace();
46             }
47             try {
48                 rs.close();
49             } catch (SQLException e) {
50                 // TODO Auto-generated catch block
51                 e.printStackTrace();
52             }
53             MysqlDB.closeStmt();
54             MysqlDB.closeConn();
55         }else{
56             validate = true;
57         }
58         if(validate){
59             response.sendRedirect("/jsp_model2/success.jsp");
60         }else{
61             response.sendRedirect("/jsp_model2/error.jsp");
62         }
63     }
64     public void doPost(HttpServletRequest request, HttpServletResponse response)
65             throws ServletException, IOException {
66         doGet(request, response);
67     }
68 
69 }
MainServlet.java

  修改web.xml文件,代码如下:

 1 xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3   <display-name>jsp_model2display-name>
 4   <servlet>
 5     <servlet-name>MainServletservlet-name>
 6     <servlet-class>com.ddpapa.test.model.servlet.MainServletservlet-class>
 7   servlet>
 8 
 9   <servlet-mapping>
10     <servlet-name>MainServletservlet-name>
11     <url-pattern>/servlet/MainServleturl-pattern>
12   servlet-mapping>
13   <welcome-file-list>
14     <welcome-file>login.jspwelcome-file>
15   welcome-file-list>
16 web-app>

  修改login.jsp并删除valitade.jsp文件,代码如下:

 1 <%@ page language="java" pageEncoding="gb2312" %>
 2 <html>
 3 <head>
 4     <title>登陆界面title>
 5 head>
 6 <body>
 7 <form action="servlet/MainServlet" method="get">
 8     <table>
 9         <tr>
10             <th colspan="2" align="center">登陆页面th>
11         tr>
12         <tr>
13             <td>用户名:td>
14             <td><input type="text" name="username" size="20">td>
15         tr>
16         <tr>
17             <td>密码:td>
18             <td><input type="password" name="password" size="20">td>
19         tr>
20     table>
21     <input type="submit" value="登陆">
22     <input type="reset" value="重置">
23 form>
24 body>
25 html>

  错误集锦:

JavaEE——JSP开发模式_第8张图片

login.jsp中的action没有和servlet中的url-pattern匹配上

servlet中的url-pattern匹配问题参考:

https://www.cnblogs.com/canger/p/6084846.html

https://blog.csdn.net/jarniyy/article/details/74295802

你可能感兴趣的:(JavaEE——JSP开发模式)