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">默认风格 </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
本文地址:http://com.8s8s.com/it/it15933.htm