structs2学习-day01

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了structs2学习-day01相关的知识,希望对你有一定的参考价值。

要求:

完成Java Web项目开发环境的安装:
JDK的安装
Tomcat的安装
MyEclipse的安装(包括MyEclipse用激活工具激活)
在MyEclipse中配置Tomcat、配置Tomcat的JDK、测试MyEclipse中配置的Tomcat是否配置成功
安装mysql的服务器端、安装mysql的图形用户界面
在安装好的图形用户界面中连接mysql服务器并创建一个数据库,在数据库中创建一个user表(包含username、password、status、age、address五个字段,增加五条记录)

创建一个Web Project,基于MVC设计模式,使用JSP/Servlet及JDBC实现读取数据库user表中存放的数据并以表格的形式展示在页面上的功能。

 

ConnectionFactory.java

技术分享
 1 public class ConnectionFactory {
 2     private static Connection conn;
 3     public static Connection getConn(){
 4 //        加载驱动类
 5         try {
 6             Class.forName("com.mysql.jdbc.Driver");
 7 //            建立与数据库的连接
 8             conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/demo?useUnicode=true&" +
 9                     "characterEncoding=utf-8", "root", "123");
10         } catch (ClassNotFoundException e) {
11             // TODO Auto-generated catch block
12             e.printStackTrace();
13         } catch (SQLException e) {
14             // TODO Auto-generated catch block
15             e.printStackTrace();
16         }
17 
18         return conn;
19     }
20 }
View Code

User.java

技术分享
 1 public class User {
 2     private String username;
 3     private String password;
 4     private int status;
 5     public String getUsername() {
 6         return username;
 7     }
 8     public void setUsername(String username) {
 9         this.username = username;
10     }
11     public String getPassword() {
12         return password;
13     }
14     public void setPassword(String password) {
15         this.password = password;
16     }
17     public int getStatus() {
18         return status;
19     }
20     public void setStatus(int status) {
21         this.status = status;
22     }
23     
24 }
View Code

UserDao.java

技术分享
 1 public class UserDao {
 2     public User getUserByName(User user){
 3 //        建立连接
 4         Connection conn = ConnectionFactory.getConn();
 5         String sql="select * from user where username = ?";
 6 //        获取预编译语句对象
 7         try {
 8             PreparedStatement ps = conn.prepareStatement(sql) ;
 9             ps.setString(1, user.getUsername());
10             ResultSet rs = ps.executeQuery();
11             while(rs.next()){
12                 User result = new User();
13                 result.setUsername(rs.getString("username"));
14                 result.setPassword(rs.getString("password"));
15                 result.setStatus(rs.getInt("status"));
16                 return result;
17             }
18         } catch (SQLException e) {
19             // TODO Auto-generated catch block
20             e.printStackTrace();
21         }
22         return null;
23     }
24 }
View Code

UserService.java

技术分享
 1 public class UserService {
 2     UserDao ud = new UserDao();
 3     public int getUserByName(User user){
 4         int flag = 0; 
 5         User result = ud.getUserByName(user);
 6         if(result!=null && !"".equals(result)){
 7             if(result.getPassword().equals(user.getPassword())){
 8                 if(result.getStatus()==0){
 9                     flag = 0;//用户信息正在审核中
10                 }else if(result.getStatus()==1){
11                     flag = 1;//用户信息审核通过
12                 }else if(result.getStatus()==2){
13                     flag = 2;//用户信息审核未通过
14                 }
15             }else{
16                 flag = 3;//用户密码错误
17             }
18         }else{
19             flag = 4;//用户名错误
20         }
21         
22         
23         return flag;
24     }
25 }
View Code

LoginServlet.java

技术分享
 1 public class LoginServlet extends HttpServlet {
 2     UserService us = new UserService();
 3 
 4     public void doGet(HttpServletRequest request, HttpServletResponse response)
 5             throws ServletException, IOException {
 6 
 7         response.setContentType("text/html");
 8         this.doPost(request, response);
 9     }
10 
11     /**
12      * The doPost method of the servlet. <br>
13      *
14      * This method is called when a form has its tag value method equals to post.
15      * 
16      * @param request the request send by the client to the server
17      * @param response the response send by the server to the client
18      * @throws ServletException if an error occurred
19      * @throws IOException if an error occurred
20      */
21     public void doPost(HttpServletRequest request, HttpServletResponse response)
22             throws ServletException, IOException {
23 
24         response.setContentType("text/html;charset=utf-8");
25         
26 //        处理中文乱码
27         request.setCharacterEncoding("UTF-8");
28         response.setCharacterEncoding("UTF-8");
29         
30 //        接受请求
31         String username = request.getParameter("username");
32         String password = request.getParameter("password");
33         
34 //        把值封装到对象中
35         User user = new User();
36         user.setUsername(username);
37         user.setPassword(password);
38         
39 //        处理请求
40         int result = us.getUserByName(user);
41         
42 //        实现跳转
43         if(result == 0){
44             request.setAttribute("msg", "用户信息正在审核中!");
45             request.getRequestDispatcher("login.jsp").forward(request, response);
46         }else if(result == 1){
47             request.setAttribute("msg", "用户登录成功!");
48             request.getRequestDispatcher("main.jsp").forward(request, response);
49         }else if(result == 2){
50             request.setAttribute("msg", "用户信息审核失败!");
51             request.getRequestDispatcher("login.jsp").forward(request, response);
52         }else if(result == 3){
53             request.setAttribute("msg", "用户密码错误!");
54             request.getRequestDispatcher("login.jsp").forward(request, response);
55         }else if(result == 4){
56             request.setAttribute("msg", "用户名错误!");
57             request.getRequestDispatcher("login.jsp").forward(request, response);
58         }
59     }
60 
61 }
View Code

login.jsp

技术分享
 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>登录页面</title>
13     
14   </head>
15   
16   <body>
17   <div>提示信息:<%=request.getAttribute("msg") %></div>
18     <form action="loginServlet" method="post">
19         <table>
20             <tr>
21                 <td>用户名:</td>
22                 <td><input type="text" name="username"/></td>
23             </tr>
24             <tr>
25                 <td>密码:</td>
26                 <td><input type="password" name="password"/></td>
27             </tr>
28             <tr>
29                 <td></td>
30                 <td><input type="submit" value="登陆"/></td>
31             </tr>
32         </table>
33     
34     </form>
35   </body>
36 </html>
View Code

main.jsp

技术分享
 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6 
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11     
12     <title>登录页面</title>
13     
14   </head>
15   
16   <body>
17         登录成功!<br/>
18         欢迎<%=request.getParameter("username") %>登录!
19         
20   </body>
21 </html>
View Code

 







以上是关于structs2学习-day01的主要内容,如果未能解决你的问题,请参考以下文章

spring系统学习:day1--spring介绍

码农小汪-struts2学习2-Structs2简单使用流程

structs2和lo4j2的问题

Python学习笔记-基础Day01

apriori片段代码

学习笔记:python3,代码片段(2017)