sxymvc

 

1.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>My JSP 'login.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26     <form action="LoginServlet" method="post">
27         <table>
28             <tr>
29                 <td>用户名 :</td>
30                 <td><input type="text" name="username">
31                 </td>
32             </tr>
33             <tr>
34                 <td>密码 :</td>
35                 <td><input type="text" name="password">
36                 </td>
37             </tr>
38             <tr>
39                 <td colspan="2"><input type="submit" value="提交"></td>
40             </tr>
41         </table>
42     </form>
43   </body>
44 </html>

2.welcome.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>My JSP 'weclome.jsp' starting page</title>
13     
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">    
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22 
23   </head>
24   
25   <body>
26    welcome my friend <B>${username}</B> ! <br>
27   </body>
28 </html>

3.UserServlet

 

 

 1 package com.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 import com.bean.User;
12 import com.service.UserService;
13 
14 public class LoginServlet extends HttpServlet {
15 
16     public void doGet(HttpServletRequest request, HttpServletResponse response)
17             throws ServletException, IOException {
18 
19         doPost(request,response);
20     }
21 
22     public void doPost(HttpServletRequest request, HttpServletResponse response)
23             throws ServletException, IOException {
24 
25         String username = request.getParameter("username");
26         String password = request.getParameter("password");
27         
28         User user = new User();
29         user.setUsername(username);
30         user.setPassword(password);
31         
32         //调用service进行业务逻辑处理
33         UserService userService = new UserService();
34         boolean isExist = userService.getUser(user);
35         
36         if(isExist){
37             request.setAttribute("username",username);
38             request.getRequestDispatcher("jsp/weclome.jsp").forward(request, response);
39         }else{
40             request.getRequestDispatcher("jsp/login.jsp").forward(request, response);
41         }
42     }
43 
44 
45 }

 

4.UserService.java

 1 package com.service;
 2 
 3 import com.bean.User;
 4 import com.dao.UserDao;
 5 
 6 
 7 public class UserService {
 8     
 9     public boolean getUser(User user){
10         //查询数据库user对象是否存在
11         UserDao userdao= new UserDao();
12         //去数据库查询user是否存在
13         return userdao.getUser(user);
14         
15         
16     }
17 
18 
19 }

5.UserDao.java

 

 

 1 package com.dao;
 2 
 3 import java.sql.*;
 4 
 5 import com.bean.User;
 6 
 7 public class UserDao {
 8     
 9     public boolean getUser(User user){
10         boolean isExist=false;
11         Connection coon=null;
12         Statement sta = null;
13         ResultSet re = null;
14         String url="jdbc:oracle:thin:@localhost:1521:inspur";
15         String name="scott";
16         String pass="tiger";
17         try {
18             
19             //加载驱动
20             Class.forName("oracle.jdbc.driver.OracleDriver");
21             //建立连接
22             coon=DriverManager.getConnection(url,name,pass);
23             //创建Statement对象,执行sql
24             sta=coon.createStatement();
25             String sql="select * from loginuser where username = '"+user.getUsername()+"' and password ='"+user.getPassword()+"'";
26             re=sta.executeQuery(sql);
27             //处理结果
28             if(re.next()){
29                 isExist=true;
30             }
31             //释放资源
32             re.close();
33                 sta.close();
34                 coon.close();
35             
36         } catch (Exception e) {
37             // TODO: handle exception
38             e.printStackTrace();
39         }
40         
41         return isExist;
42     }
43 
44 }

6.User.java 

 

 

 

 1 package com.bean;
 2 
 3 public class User {
 4     private String username;
 5     private String password;
 6     public String getUsername() {
 7         return username;
 8     }
 9     public void setUsername(String username) {
10         this.username = username;
11     }
12     public String getPassword() {
13         return password;
14     }
15     public void setPassword(String password) {
16         this.password = password;
17     }
18     
19 
20 }
posted @ 2018-05-24 21:10  helloWorldhelloWorld  阅读(202)  评论(0)    收藏  举报