Jsp & amp; servlet initial experience-user login function implementation,

Source: Internet
Author: User

Jsp & servlet initial experience-user login function implementation,
Database preparation-create the t_user table of the db_login Database

 

1. Create a web Project 2. Create a user model user. java
 1 package com.gxy.model; 2  3 public class User { 4     private int id; 5     private String userName; 6     private String password; 7      8     public User() { 9         super();10     }11     12     13     public User(String userName, String password) {14         super();15         this.userName = userName;16         this.password = password;17     }18 19 20     public int getId() {21         return id;22     }23     public void setId(int id) {24         this.id = id;25     }26     public String getUserName() {27         return userName;28     }29     public void setUserName(String userName) {30         this.userName = userName;31     }32     public String getPassword() {33         return password;34     }35     public void setPassword(String password) {36         this.password = password;37     }38 39 }
3. Create the util package Dbutil. java
 1 package com.gxy.util; 2  3 import java.sql.Connection; 4 import java.sql.DriverManager; 5  6 public class Dbutil { 7     private String dbUrl ="jdbc:mysql://localhost:3306/db_login"; 8     private String jdbcName="com.mysql.jdbc.Driver"; 9     private String dbUserName="root";10     private String dbpassword="123456";11     12     public Connection getcon() throws Exception{13         Class.forName(jdbcName);14         Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbpassword);15         return con;16     }17     18     public void closeCon(Connection con) throws Exception{19         con.close();20     }21 22 }
4. Create the dao package UserDao. java
 1 package com.gxy.dao; 2  3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6  7 import com.gxy.model.User; 8  9 public class UserDao {10     public User login(Connection con,User user) throws Exception{11         User resultUser=null;12         String sql="select * from t_user where userName=? and passWord=?";13         PreparedStatement pst=con.prepareStatement(sql);14         pst.setString(1, user.getUserName());15         pst.setString(2,user.getPassword());16         ResultSet rs=pst.executeQuery();17         if(rs.next()){18             resultUser=new User();19             resultUser.setUserName(rs.getString("userName"));20             resultUser.setPassword(rs.getString("passWord"));21         }22         return resultUser;23     }24 25 }
5. Create a servlet package loginServlet
 1 package com.gxy.servlet; 2  3 import java.io.IOException; 4 import java.sql.Connection; 5  6 import javax.servlet.RequestDispatcher; 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.gxy.dao.UserDao;14 import com.gxy.model.User;15 import com.gxy.util.Dbutil;16 17 public class loginServlet extends HttpServlet{18 19     /**20      * 21      */22     private static final long serialVersionUID = 1L;23 24     Dbutil dbutil=new Dbutil();25     UserDao userDao=new UserDao();26     @Override27     protected void doGet(HttpServletRequest req, HttpServletResponse resp)28             throws ServletException, IOException {29         this.doPost(req, resp);30     }31 32     @Override33     protected void doPost(HttpServletRequest req, HttpServletResponse resp)34             throws ServletException, IOException {35         String userName=req.getParameter("userName");36         String passWord=req.getParameter("passWord");37         38         Connection con=null;39         try {40             User user=new User(userName,passWord);41             con=dbutil.getcon();42             User resultUser=userDao.login(con, user);43             if(resultUser==null){44                 System.out.println("no");45             }else{46                 HttpSession session=req.getSession();47                 session.setAttribute("userName", resultUser.getUserName());48                 session.setAttribute("passWord", resultUser.getPassword());49                 resp.sendRedirect("target.jsp");50             }51         } catch (Exception e) {52             // TODO Auto-generated catch block53             e.printStackTrace();54         }55         56     }57 58 }
6. 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 Jump to the target. jsp page
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 7. Running result

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.