JavaEE——JSP开发模式(model1)

2019-10-17 09:05:24来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

JavaEE——JSP开发模式(model1)

model1开发模式

  • 工作流程:

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

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

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

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

  • JSP的理解

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

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

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

  建立基于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_model1</display-name>
4   <welcome-file-list>
5     <welcome-file>login.jsp</welcome-file>
6   </welcome-file-list>
7 </web-app>
  • 错误集锦

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

  

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

  success代码如下时出现错误:

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


原文链接:https://www.cnblogs.com/ddpapa/p/11687212.html
如有疑问请与原作者联系

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:mybatis中一对一关系映射

下一篇:Spring Boot 定时任务 @Scheduled