分享
 
 
 

Servlet学习笔记(四)-----使用Servlet处理用户注册和登陆

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

1. 首先是login.html页面,代码如下:

<html>

<body>

<script language="JavaScript">

function valid(form)

{

if(form.username.value.length==0)

{

alert("Please enter username!");

form.username.focus();

return false;

}

if(form.pass.value.length==0)

{

alert("Please enter password!");

form.pass.focus();

return false;

}

}

</script>

<form onsubmit="return valid(this)" method=post action="http://127.0.0.1:8000/web/webAliases">

<table height="150" border="0" align="center">

<tr>

<td>用户名:

<input name=username type=text size=20>

<font color=#666666 size="4">

</font>

<br>

<br>

密码:

<input name=pass type=password size=20>

<br>

<br>

<font color=#000000>风格:</font>

<select class=border1 name="style">

<option class=inpt value="1">随心DIY</option>

<option class=inpt value="-1">默认风格&nbsp;</option>

<option class=inpt value="0">简约风格</option>

</select>

<input name="enter" type=image src="but-login.gif" alt="登录" align=absmiddle width="54" height="23" border=0>

<a href="http://127.0.0.1:8000/web1/RegistorForm.htm"><img src="but-register.gif" alt="注册新用户" width="83" height="23" border=0 align=absmiddle></a>

</td>

</tr>

</table>

</form>

</body>

</html>

2. RegistorForm.html页面,代码如下:

<html>

<body bgcolor=pink>

<script language="JavaScript">

function valid(form)

{

if(form.username.value.length==0)

{

alert("The user name cannot be left black!");

form.username.focus();

return false;

}

if(form.pass.value.length==0)

{

alert("The pass cannot be left black!");

form.pass.focus();

return false;

}

if(form.address.value.length==0)

{

alert("The Address cannot be left black!");

form.address.focus();

return false;

}

if(form.acctype.value.length==0)

{

alert("The Account Type cannot be left black!");

form.acctype.focus();

return false;

}

if(form.income.value.length==0)

{

alert("The Annual Income cannot be left black!");

form.income.focus();

return false;

}

if(form.phonenumber.value.length==0)

{

alert("The Phone Number cannot be left black!");

form.phonenumber.focus();

return false;

}

}

</script>

<form onsubmit="return valid(this)" method=post action="http://127.0.0.1:8000/web1/web1Aliases">

<center>

<br><br><br><br><br><br>

<table border=1>

<center>

<tr>

<td>用户名:</td>

<td><input type=text name="username"></td>

</tr>

<tr>

<td>密码:</td>

<td><input type=text name="pass"></td>

</tr>

<tr>

<td>地址:</td>

<td><input type=text name="address"></td>

</tr>

<tr>

<td>帐户类型:</td>

<td><input type=text name="acctype"></td>

</tr>

<tr>

<td>收入:</td>

<td><input type=text name="income"></td>

</tr>

<tr>

<td>电话号码:</td>

<td><input type=text name="phonenumber"></td>

<tr>

<td colspan=2><center><input type=submit value=submit></center></td>

</tr>

</table>

</center>

</form>

</body>

</html>

3. LoginForm.java代码如下:

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.sql.*;

///import javax.sql.*;

public class LoginForm extends HttpServlet

{

Connection dbcon;

protected void doPost(HttpServletRequest req,

HttpServletResponse resp)

throws ServletException,

java.io.IOException

{

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

dbcon=DriverManager.getConnection("jdbc:odbc:test","sa","");

System.out.println("Connection established");

}

catch(Exception e)

{

System.out.println("Database not found!");

}

String username=req.getParameter("username").trim();

String pass=req.getParameter("pass").trim();

resp.setContentType("text/html");

PrintWriter out=resp.getWriter();

try

{

PreparedStatement stat=dbcon.prepareStatement(

"select * from UserData where username=? and pass=?");

stat.setString(1,username);

stat.setString(2,pass);

ResultSet result=stat.executeQuery();

if(result.next())

{

out.println("<html><body bgcolor=pink>");

out.println(" Login Successful");

out.println("<br><br>");

out.println("</body></html>");

}

else

{

out.println("<html><body bgcolor=pink>");

out.println(" Login ERROR");

out.println("<br><br>");

out.println("</body></html>");

}

}

catch(Exception e)

{

e.toString();

}

try

{

dbcon.close();

}

catch(Exception e)

{

System.out.println(e.toString());

}

}

}

4. RegistorForm.java代码如下:

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.sql.*;

///import javax.sql.*;

public class RegistorForm extends HttpServlet

{

Connection dbcon;

protected void doPost(HttpServletRequest req,

HttpServletResponse resp)

throws ServletException,

java.io.IOException

{

try

{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

dbcon=DriverManager.getConnection("jdbc:odbc:test","sa","");

System.out.println("Connection established");

}

catch(Exception e)

{

System.out.println("Database not found!");

}

resp.setContentType("text/html");

PrintWriter out=resp.getWriter();

String firstname=req.getParameter("username");

String lastname=req.getParameter("pass");

String address=req.getParameter("address");

String acctype=req.getParameter("acctype");

double income=Double.parseDouble(req.getParameter("income"));

int phonenumber=Integer.parseInt(req.getParameter("phonenumber"));

int rows=0;

try

{

PreparedStatement stat=dbcon.prepareStatement(

"insert UserData values(?,?,?,?,?,?)");

stat.setString(1,firstname);

stat.setString(2,lastname);

stat.setString(3,address);

stat.setString(4,acctype);

stat.setDouble(5,income);

stat.setInt(6,phonenumber);

rows=stat.executeUpdate();

}

catch(Exception e)

{

System.out.println(e.toString());

}

if(rows==0)

{

System.out.println("Error Insert");

}

else

{

System.out.println("Successful Insert");

}

String regno=new String("1111");

try

{

PreparedStatement stat=dbcon.prepareStatement(

"select max(UserID) from UserData");

ResultSet result=stat.executeQuery();

result.next();

regno=result.getString(1);

}

catch(Exception e)

{

e.toString();

}

out.println("<html><body bgcolor=pink>");

out.println("Thank you");

out.println("<br><br>");

out.println("your registration ID is :<b>"+regno+"</b>");

out.println("<br>");

out.println("</body></html>");

try

{

dbcon.close();

}

catch(Exception e)

{

System.out.println(e.toString());

}

}

}

5. 数据库UserData.sql代码如下:

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UserData]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

drop table [dbo].[UserData]

GO

CREATE TABLE [dbo].[UserData] (

[UserID] [int] IDENTITY (1, 1) NOT NULL ,

[username] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,

[pass] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,

[address] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,

[acctype] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,

[income] [money] NULL ,

[phonenumber] [int] NULL

) ON [PRIMARY]

GO

6. 因为有两个Servlet,所以需要新建两个Web Component,我把它们命名为web和web1

7. J2ee服务器启动后,客户端输入http://127.0.0.1:8000/web/Login.htm

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有