JSPJavaBeanServlet實現(xiàn)用戶登錄和注冊_第1頁
JSPJavaBeanServlet實現(xiàn)用戶登錄和注冊_第2頁
JSPJavaBeanServlet實現(xiàn)用戶登錄和注冊_第3頁
JSPJavaBeanServlet實現(xiàn)用戶登錄和注冊_第4頁
JSPJavaBeanServlet實現(xiàn)用戶登錄和注冊_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、JSP JavaBean Servlet實現(xiàn)用戶登錄和注冊1 數(shù)據(jù)庫:create database student;use student;create table user(id int primary key auto_increment,user varchar(50) not null,pwd varchar(50) not null,name varchar(50) not null,* varchar(50) not null,age int(50) not null); 2 index.jsp <% page language="java" impor

2、t="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme() ":/" request.getServerName() ":" request.getServerPort() path "/"%><!DOCTYPE HTML PUBLIC "-/W3C/DTD HTML 4.01 Transi

3、tional/EN"><html><head> <base href="<%=basePath%>"> <title>歡迎來到學生管理系統(tǒng)</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv=

4、"expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"></head><body> <div align="center"> <font size=&qu

5、ot; 2" color="#FF6633">用戶登錄</font></div><form id="form1" name="form1" method="post" action="loginServlet"><table width="357" border="0" align="center"> <tr> <td width="128&qu

6、ot;>用戶名:</td> <td width="219"><label> <input name="user" type="text" id="user" /> </label></td> </tr> <tr> <td>密碼:</td> <td><label> <input name="pwd" type="password&qu

7、ot; id="pwd" /> </label></td> </tr> <tr> <td><label> <input type="submit" name="Submit" value="登錄" /> </label></td> <td><label><a href="addUser.jsp">用戶注冊</a></label&g

8、t;</td> </tr></table><p>&nbsp;</p></form></body></html>3 addUser.jsp <% page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getSch

9、eme() ":/" request.getServerName() ":" request.getServerPort() path "/"%><!DOCTYPE HTML PUBLIC "-/W3C/DTD HTML 4.01 Transitional/EN"><html><head> <base href="<%=basePath%>"> <title>用戶注冊</title> <meta ht

10、tp-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><m

11、eta http-equiv="description" content="This is my page"></head><body> <p align="center"><font size=" 3" color="#FF3300">用戶注冊</font></p><form id="form1" name="form1" method="post"

12、action="addUserServlet"><table width="340" border="0" align="center"> <tr> <td width="141">用戶名:</td> <td width="189"><label> <input name="user" type="text" id="user" /&

13、gt; </label></td> </tr> <tr> <td>密碼:</td> <td><label> <input name="pwd" type="password" id="pwd" /> </label></td> </tr> <tr> <td>重輸密碼:</td> <td><label> <input name=&

14、quot;pwd1" type="password" id="pwd1" /> </label></td> </tr> <tr> <td>姓名:</td> <td><label> <input name="name" type="text" id="name" /> </label></td> </tr> <tr> <t

15、d>性別:</td> <td><label> <input name="*" type="radio" value="男" checked="checked" /> 男 <input type="radio" name="*" value="女" /> 女</label></td> </tr> <tr> <td>年齡:</td

16、> <td><label> <input name="age" type="text" id="age" /> </label></td> </tr> <tr> <td><label> <input type="submit" name="Submit" value="提交" /> </label></td> <td>

17、;<label> <input type="reset" name="Submit2" value="重置" /> </label></td> </tr></table></form></body></html> 4 DAO.java 實現(xiàn)與數(shù)據(jù)庫的連接用戶名驗證與用戶名新建添加 package dao;import getConnection.GetConnection;import java.sql.Connection;i

18、mport java.sql.PreparedStatement;import java.sql.SQLException;import com.mysql.jdbc.ResultSet;import bean.User;public class Dao private Connection conn;private PreparedStatement pstat;String sql=""/* * 用戶登錄*/public boolean logoin(User user) throws SQLException conn = GetConnection.getConne

19、ction(); boolean i = false; sql = "select * from user where user=? and pwd=?" pstat = conn.prepareStatement(sql); pstat.setString(1, user.getUser(); pstat.setString(2, user.getPwd(); ResultSet rs1 = (ResultSet) pstat.executeQuery(); if (rs1.next() i = true; rs1.close(); pstat.close(); else

20、 i = false ; rs1.close(); pstat.close(); conn.close(); return i;/* 用戶注冊*/public void addUser(User user) conn = GetConnection.getConnection(); sql = "insert into user values(?,?,?,?,?)" try pstat = conn.prepareStatement(sql); pstat.setString(1,user.getUser(); pstat.setString(2,user.getPwd()

21、; pstat.setString(3,user.getName(); pstat.setString(4,user.get*(); pstat.setInt(5,user.getAge(); pstat.executeUpdate(); pstat.close(); conn.close(); catch(SQLException e) e.printStackTrace(); 5 User.java package bean;public class User private String user;private String pwd;private String name;privat

22、e String *;private int age;public String getUser() return user;public void setUser(String user) this.user = user;public String getPwd() return pwd;public void setPwd(String pwd) this.pwd = pwd;public String getName() return name;public void setName(String name) = name;public String get*()

23、return *;public void set*(String *) this.* = *;public int getAge() return age;public void setAge(int age) this.age = age; 6 GetConnection.java 實現(xiàn)與數(shù)據(jù)庫之間的連接 package getConnection;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;/* * 用JDBC的方法獲得數(shù)據(jù)庫的連接*/public class G

24、etConnection /通過靜態(tài)方法注冊驅(qū)動,獲得連接public static Connection getConnection() String driver = "com.mysql.jdbc.Driver" String url = "jdbc:mysql:/localhost/student" Connection con = null; try Class.forName(driver); try con = DriverManager.getConnection(url,"root",""); c

25、atch (SQLException e) e.printStackTrace(); catch (ClassNotFoundException e) e.printStackTrace(); System.out.println("已獲得數(shù)據(jù)庫的連接"); return con;/*public static void main(String args) getConnection();*/7 addUserServlet.java package servlet;import java.io.IOException;import javax.servlet.Servle

26、tException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import dao.Dao;import bean.User;public class addUserServlet extends HttpServlet /* Destruction of the servlet. */public void destroy() super.destroy(); / Just p

27、uts "destroy" string in log / Put your code herepublic void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException response.setContentType("text/html");doPost(request,response);public void doPost(HttpServletRequest request, HttpServle

28、tResponse response) throws ServletException, IOException response.setContentType("text/html"); response.setCharacterEncoding("utf-8"); String user1 = request.getParameter("user"); String pwd = request.getParameter("pwd"); String name = new String(request.getPa

29、rameter("name").getBytes("ISO8859_1"),"utf-8"); String * = new String(request.getParameter("*").getBytes("ISO8859_1"),"utf-8"); String age1 = request.getParameter("age"); User user = new User(); user.setUser(user1); user.setPwd(pw

30、d); user.setName(name); user.set*(*); int age; if(age1!=null) age = Integer.parseInt(age1); user.setAge(age); Dao dao = new Dao(); dao.addUser(user);/* request.setAttribute("info",new String("添加成功!恭喜!" + ""); request.setAttribute("id", new String("a"

31、); request.setAttribute("denglu",new String("登陸");*/ request.getRequestDispatcher("info.jsp").forward(request,response);public void init() throws ServletException / Put your code here 8 loginServlet.java package servlet;import java.io.IOException;import java.io.PrintWriter;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import bean.User;import dao.Dao;public class loginServlet extends H

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論