導航:首頁 > 編程語言 > servlet用戶注冊代碼

servlet用戶注冊代碼

發布時間:2023-09-21 04:19:25

『壹』 用java web編寫一個用戶注冊界面(只要寫出以下要求就行)

一步步更新:頁面
<form action="regist.servlet" method="post"><table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" class="tableAdd borTop"> <tr> <th width="14%" height="30" nowrap>用戶名</th> <td class="pl5"> <INPUT id="sName" name="name" type="text" size="20"> </td> </tr> <tr> <th width="14%" height="30" nowrap>密碼</th> <td class="pl5"> <INPUT name="password" type="password" size="20"> </td> </tr> <tr> <th width="14%" height="30" nowrap>確認密碼</th> <td class="pl5"> <INPUT name="confrimPwd" type="password" size="20"> </td> </tr> <tr> <th width="14%" height="30" nowrap>性別</th> <td class="pl5"> 男<INPUT name="sex" type="radio" value="1" checked="checked" size="20"> 女<INPUT name="sex" type="radio" value="0" size="20"> </td> </tr> <tr> <th width="14%" height="30" nowrap>愛好</th> <td class="pl5"> <INPUT name="enjoy" type="checkbox" size="20" value="籃球">籃球 <INPUT name="enjoy" type="checkbox" size="20" value="足球">足球 </td> </tr> <tr> <th width="14%" height="30" nowrap>生日</th> <td class="pl5"> <INPUT name="brithday" type="text" size="20"> </td> </tr> <tr> <th width="14%" height="30" nowrap>備注</th> <td class="pl5"> <textarea rows="5" cols="200" name="remark"></textarea> </td> </tr> <tr> <th width="14%" height="30" nowrap> </th> <td class="pl5"> <input type="submit" value="提交"> <input type="reset" value="重置"> </td> </tr></table></form>

資料庫部分:
import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import com.mysql.jdbc.Connection;import com.mysql.jdbc.Statement;public class DataBaseUtil { public static Connection getConnection() throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://192.168.100.113/datebase", "username", "password"); return conn; } public static Statement getPS() throws ClassNotFoundException, SQLException { Statement statement = (Statement) getConnection().createStatement(); return statement; } public static void close(Connection conn,Statement st,ResultSet rs) throws SQLException{ if(rs != null) { rs.close(); } if(st != null) { st.close(); } if(conn != null) { conn.close(); } }}

『貳』 如何用jsp jdbc servlet實現登錄注冊

第一步:web.xml

Java code?

<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"xmlns="
xmlns:xsi="
xsi:schemaLocation="

<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>

<servlet>
<description></description>
<display-name></display-name>
<servlet-name>loginServlet</servlet-name>
<servlet-class>com.servlet.loginServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>loginServlet</servlet-name>
<url-pattern>/loginServlet</url-pattern>
</servlet-mapping>
</web-app>




第二步:資料庫

Java code?

/*
SQLyogUltimatev8.32
MySQL-5.5.23:Database-student
*********************************************************************
*/


/*!40101SETNAMESutf8*/;

/*!40101SETSQL_MODE=''*/;

/*!40014SET@OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS,UNIQUE_CHECKS=0*/;
/*!40014SET@OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS,FOREIGN_KEY_CHECKS=0*/;
/*!40101SET@OLD_SQL_MODE=@@SQL_MODE,SQL_MODE='NO_AUTO_VALUE_ON_ZERO'*/;
/*!40111SET@OLD_SQL_NOTES=@@SQL_NOTES,SQL_NOTES=0*/;
CREATEDATABASE/*!32312IFNOTEXISTS*/`student`/*!40100DEFAULTCHARACTERSETutf8*/;

USE`student`;

/*Tablestructurefortable`user`*/

DROPTABLEIFEXISTS`user`;

CREATETABLE`user`(
`id`int(11)NOTNULLAUTO_INCREMENT,
`user`varchar(50)NOTNULL,
`pwd`varchar(50)NOTNULL,
`name`varchar(50)NOTNULL,
`age`int(50)NOTNULL,
PRIMARYKEY(`id`)
)ENGINE=InnoDBAUTO_INCREMENT=2DEFAULTCHARSET=utf8;

/*Dataforthetable`user`*/

insertinto`user`(`id`,`user`,`pwd`,`name`,`age`)values(1,'zhangsan',飓','張三',21);

/*!40101SETSQL_MODE=@OLD_SQL_MODE*/;
/*!40014SETFOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS*/;
/*!40014SETUNIQUE_CHECKS=@OLD_UNIQUE_CHECKS*/;
/*!40111SETSQL_NOTES=@OLD_SQL_NOTES*/;

第三步:登錄login.jsp

Java code?

<%@pagelanguage="java"contentType="text/html;charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<title>xx系統</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>

<body>
<divalign="center"><fontsize="2"color="#FF6633">用戶登錄</font>
</div>
<formid="form1"name="form1"method="post"action="loginServlet">
<tablewidth="357"border="0"align="center">
<tr>
<tdwidth="128">用戶名:</td>
<tdwidth="219"><label>
<inputname="user"type="text"id="user"/>
</label></td>
</tr>
<tr>
<td>密碼:</td>
<td><label>
<inputname="pwd"type="password"id="pwd"/>
</label></td>
</tr>
<tr>
<td><label>
<inputtype="submit"name="Submit"value="登錄"/>
</label></td>

</tr>
</table>
<p>&nbsp;</p>
</form>
</body>
</html>




第四步:success.jsp

Java code

<%@pagelanguage="java"contentType="text/html;charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=ISO-8859-1">
<title>Inserttitlehere</title>
</head>
<body>
${address}</br>
${port}</br>

</body>
</html>




第五步:loginServlet.java

Java code

packagecom.servlet;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.net.InetAddress;
importjava.sql.SQLException;

importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjavax.servlet.http.HttpSession;

importcom..Dao;

{
publicvoiddestroy(){
super.destroy();//Justputs"destroy"stringinlog

//Putyourcodehere

}

publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
request.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriterout=response.getWriter();
Stringname=newString(request.getParameter("user").getBytes(
"ISO8859_1"),"GBK");
Stringpwd=newString(request.getParameter("pwd").getBytes(
"ISO8859_1"),"UTF-8");
Useruser=newUser();
user.setUser(name);
user.setPwd(pwd);
Dao=newDao();
booleanisLogin;
try{
isLogin=.logoin(user);

if(isLogin){
InetAddressinetAddress=InetAddress.getLocalHost();
Stringm=inetAddress.getHostAddress();
intn=request.getRemotePort();
System.out.println(m+"*********"+n);
HttpSessionsession=request.getSession();
session.setAttribute("address",m);
session.setAttribute("port",n);
response.sendRedirect("success.jsp");
}else{
response.sendRedirect("index.jsp");
}

}catch(SQLExceptione){
e.printStackTrace();
}
}

publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doPost(request,response);
}

publicvoidinit()throwsServletException{
}

}

Java code

packagecom.servlet;

publicclassUser{
privateStringuser;
privateStringpwd;
privateStringname;

privateintage;

publicStringgetUser(){
returnuser;
}

publicvoidsetUser(Stringuser){
this.user=user;
}

publicStringgetPwd(){
returnpwd;
}

publicvoidsetPwd(Stringpwd){
this.pwd=pwd;
}

publicStringgetName(){
returnname;
}

publicvoidsetName(Stringname){
this.name=name;
}

publicintgetAge(){
returnage;
}

publicvoidsetAge(intage){
this.age=age;
}
}

Java code?

packagecom.util;

importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.SQLException;

publicclassGetConnection{
//通過靜態方法注冊驅動,獲得連接

(){
Stringdriver="com.mysql.jdbc.Driver";
Stringurl="jdbc:mysql://localhost/student";
Connectioncon=null;
try{
Class.forName(driver);
try{
con=DriverManager.getConnection(url,"root","123456");
}catch(SQLExceptione){
e.printStackTrace();
}
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}
System.out.println("已獲得資料庫的連接");
returncon;
}
/*publicstaticvoidmain(Stringargs[]){
getConnection();
}*/
}

Java code?

packagecom.;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;

importcom.servlet.User;
importcom.util.GetConnection;

publicclassDao{
privateConnectionconn;
privatePreparedStatementpstat;
Stringsql="";

/**
*
*用戶登錄
*/
publicbooleanlogoin(Useruser)throwsSQLException{
conn=GetConnection.getConnection();
booleani=false;
sql="select*fromuserwhereuser=?andpwd=?";

pstat=conn.prepareStatement(sql);

pstat.setString(1,user.getUser());
pstat.setString(2,user.getPwd());

ResultSetrs1=(ResultSet)pstat.executeQuery();
if(rs1.next()){
i=true;
rs1.close();
pstat.close();
}else{
i=false;
rs1.close();
pstat.close();
}
conn.close();
returni;
}

/**
*用戶注冊
*/
publicvoidaddUser(Useruser){
conn=GetConnection.getConnection();
sql="insertintouservalues(?,?,?,?)";
try{
pstat=conn.prepareStatement(sql);
pstat.setString(1,user.getUser());
pstat.setString(2,user.getPwd());
pstat.setString(3,user.getName());

pstat.setInt(5,user.getAge());
pstat.executeUpdate();
pstat.close();
conn.close();

}catch(SQLExceptione){
e.printStackTrace();
}

}
}




注意事項:1.資料庫用戶密碼;2.訪問地址。

『叄』 如何在jsp頁面上實現點擊注冊按鈕,彈出一個窗體來注冊(類似於百度貼吧的登錄和注冊),求詳細代碼和注釋

jsp中的注冊彈出新窗口是通過window.open一個新頁面來實現的。
頁面register.jsp代碼如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="cn.wy.Pet.User" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>會員注冊例子講解</title>
<style type="text/css">
<!--
.STYLE1 {
color: #FF0000;
font-weight: bold;
}
.STYLE2 {color: #FF0000}
.STYLE3 {
font-size: 18px;
font-weight: bold;
}
-->
</style>
</head>

<body style="font-size:12px">
<form id="form1" name="form1" method="post" action="<%=actionStr%>reg">
<p align="center"><br />
<span class="STYLE3">用戶注冊</span></p>
<table width="582" height="302" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#BCACD2">
<tr>
<td width="80" align="right">用戶名:</td>
<td width="496" align="left"><input name="userName" type="text" id="userName" size="16" maxlength="16" />
<span class="STYLE1">*</span> 3~16位字母或者數字(如:8hack)</td>
</tr>
<tr>
<td align="right">密碼:</td>
<td align="left"><input name="password1" type="text" id="password1" size="16" maxlength="16" />
<span class="STYLE1">* </span> 3~16位字母或者數字(如:abc123)</td>
</tr>
<tr>
<td align="right">確認密碼:</td>
<td align="left"><input name="password2" type="text" id="password2" size="16" maxlength="16" />
<span class="STYLE1">*</span> 必須和上面輸入的密碼相同</td>
</tr>
<tr>
<td align="right">電子郵件:</td>
<td align="left"><input name="email" type="text" id="email" maxlength="20" />
<span class="STYLE1">*</span> 找回密碼和聯系用(如:[email protected])</td>
</tr>
<tr>
<td align="right">聯系電話:</td>
<td align="left"><input name="tel" type="text" id="tel" size="20" maxlength="20" />
如(0871-8888888,13888853113)</td>
</tr>
<tr>
<td align="right">聯系地址:</td>
<td align="left"><input name="address" type="text" id="address" maxlength="50" /></td>
</tr>
<td height="40" colspan="2" align="center"><input type="submit" name="Submit" value="確認注冊" />
<input type="reset" name="Submit2" value="重新填寫" /></td>
</tr>
</table>
</form>
</body>
</html>
後台servlet的處理:
public class reg extends HttpServlet
{
public reg()
{
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out;
DBConnection dbc=null;
String userName;
String psd;
String email;
String tel;
String address;
int popedom;
response.setContentType("text/html;charset=UTF-8");
out = response.getWriter();
try{
dbc = new DBConnection();
PreparedStatement ps = null;
userName = request.getParameter("userName");
psd = login.encrypt(request.getParameter("password1").toString());
email = request.getParameter("email");
tel = request.getParameter("tel");
address = request.getParameter("address");
popedom = Integer.parseInt(request.getParameter("popedom"));
if (userName != null && psd != null && email != null)
{
ps = dbc.getCon().prepareStatement("insert into [User](UName,Upass,UEmail,UTel,UAddress,UPopedom) values(?,?,?,?,?,?)");
ps.setString(1, userName);
ps.setString(2, psd);
ps.setString(3, email);
ps.setString(4, tel);
ps.setString(5, address);
ps.setInt(6, popedom);
ps.execute();
System.out.print("新用戶注冊:" + request.getParameter("userName") + " ");
out.print("<script>alert('恭喜您:注冊成功!現已經登錄到網站!');history.go(-1)</script>");
}
if (dbc != null)
dbc.dbClose();
}
catch(SQLException ex)
{
out.print("<script>alert('注冊失敗!資料庫連接錯誤!');history.go(-1)</script>");
ex.printStackTrace();
if (dbc != null)
dbc.dbClose();
}
}
}

『肆』 java,用戶注冊,輸入信息後,單擊提交按鈕,彈出一個提示框


後台方法:

private HttpServletResponse response = ServletActionContext.getResponse(); // 用戶登錄 public String login() throws IOException { System.out.println("login"); // md5加密密碼 MD5 md5 = new MD5(password); String pas = md5.compute(); Users u = new Users(); u.setUsername(username); u.setPassword(pas); //使用ajax調用返回值 response.setContentType("text/plain;charset=UTF-8"); response.setHeader("pragma", "no-cache"); response.setHeader("cache-control", "no-cache"); PrintWriter write = response.getWriter(); // 用戶用戶登錄--存在返回id,不存在或者 密碼錯誤返回-1鎖定狀態返回-2 int res = manager.checkUser(u); // System.out.println(res + "useraction"); if (res == -1) { String str = "{"success":false,"msg":"您輸入用戶不存在或者密碼錯誤,請重新輸入"}"; write.write(str); write.flush(); write.close(); return null; } if (res == -2) { // System.out.println("zhanghaosuoding"); String str = "{"success":false,"msg":"您輸入的賬號已經被鎖定"}"; write.write(str); write.flush(); write.close(); return null; } Users u2 = manager.findOneUsers(res); // System.out.println(u2.getUsername()+"-"+u2.getPassword()); // 把用戶信息和上次登錄時間放到session中,在頁面上顯示 ActionContext.getContext().getSession().put("user", u2); ActionContext.getContext().getSession().put("lastlogin", u2.getLastLoginTime().toString().substring(0, 10)); // 更新這次登錄時間到資料庫 java.sql.Timestamp lastLogin = new java.sql.Timestamp( new java.util.Date().getTime()); u2.setLastLoginTime(lastLogin); manager.updateUser(u2); String str = "{"success":true,"msg":"登陸成功"}"; write.write(str); write.flush(); write.close(); return null;

閱讀全文

與servlet用戶注冊代碼相關的資料

熱點內容
買房哪個網站好 瀏覽:913
打完新冠疫苗下載什麼app可以查到 瀏覽:879
海信電視用哪個app看網路電視 瀏覽:96
編程什麼時候流行的 瀏覽:683
自學編程新手看什麼書 瀏覽:180
linux全盤tar 瀏覽:454
ps文件命名自動輸入怎麼辦 瀏覽:467
iphone6plus切圖 瀏覽:822
iphone6沒有提示更新 瀏覽:41
cc網路圖教程 瀏覽:650
u盤無法剪切文件到電腦里 瀏覽:497
中海達靜態數據大概多少內存 瀏覽:599
蘋果6s手機文件管理器 瀏覽:107
qq頭像非主流女生捂臉 瀏覽:736
java判斷string編碼 瀏覽:941
excel工資簿如何匹配相同數據 瀏覽:159
視頻課程學習有哪些app 瀏覽:375
鐵模編程怎麼學 瀏覽:298
數學網路研修研究問題有哪些 瀏覽:677
stl文件怎麼列印 瀏覽:427

友情鏈接