|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
最后就是我对java的几点希望:首先是IDE工具,有人说java已经很好了,有jbuilder,eclipse,netBeans等等,但是我认为如果java想超越.net,那么他首先要解决的就是IDE工具的整合。js|servlet|用户注册imagebear
版权:imagebear
本例必要的软件和运转情况:
1、Windows2000Server操纵体系
2、jdk1.4
3、JCreator2.5(java源码编纂调试器,吐血保举!)
4、MacromediaJRunMX
5、MacromediaDreamweaverMX(非必须)
6、MySQL数据库(最好安装MySQLControlCenter)
1、数据库计划
用MySQLControlCenter翻开MySQL数据库,新建数据库shopping,在其下新建表tbl_user,个中各字段设置以下:
2、编写毗连数据库bean:DBConn.java
//DBConn.java
//includerequiredclasses
importjava.sql.*;
//==========================================
//DefineClassDBConn
//==========================================
publicclassDBConn
{
publicStringsql_driver="org.gjt.mm.mysql.Driver";
publicStringsql_url="jdbc:mysql://localhost:3306";
publicStringsql_DBName="shopping";
publicStringuser="sa";
publicStringpwd="";
Connectionconn=null;
Statementstmt=null;
ResultSetrs=null;
publicbooleansetDriver(Stringdrv)
{
this.sql_driver=drv;
returntrue;
}
publicStringgetDriver()
{
returnthis.sql_driver;
}
publicbooleansetUrl(Stringurl)
{
this.sql_url=url;
returntrue;
}
publicbooleansetDBName(Stringdbname)
{
this.sql_DBName=dbname;
returntrue;
}
publicStringgetDBName()
{
returnthis.sql_DBName;
}
publicbooleansetUser(Stringuser)
{
this.user=user;
returntrue;
}
publicStringgetUser()
{
returnthis.user;
}
publicbooleansetPwd(Stringpwd)
{
this.pwd=pwd;
returntrue;
}
publicStringgetPwd()
{
returnthis.pwd;
}
publicDBConn()
{
try{
Class.forName(sql_driver);//加载数据库驱动程序
this.conn=DriverManager.getConnection(sql_url+"/"+sql_DBName+"?user="+user+"&password="+pwd+"&useUnicode=true&characterEncoding=gb2312");
this.stmt=this.conn.createStatement();
}catch(Exceptione){
System.out.println(e.toString());
}
}
//实行查询操纵
publicResultSetexecuteQuery(StringstrSql)
{
try{
this.rs=stmt.executeQuery(strSql);
returnthis.rs;
}catch(SQLExceptione){
System.out.println(e.toString());
returnnull;
}catch(NullPointerExceptione){
System.out.println(e.toString());
returnnull;
}
}
//实行数据的拔出、删除、修正操纵
publicbooleanexecute(StringstrSql)
{
try{
if(this.stmt.executeUpdate(strSql)==0)
returnfalse;
else
returntrue;
}catch(SQLExceptione){
System.out.println(e.toString());
returnfalse;
}catch(NullPointerExceptione){
System.out.println(e.toString());
returnfalse;
}
}
//了局集指针跳转到某一行
publicbooleanrs_absolute(introw)
{
try{
this.rs.absolute(row);
returntrue;
}catch(SQLExceptione){
System.out.println(e.toString());
returnfalse;
}
}
publicvoidrs_afterLast()
{
try{
this.rs.afterLast();
}catch(SQLExceptione){
System.out.println(e.toString());
}
}
publicvoidrs_beforeFirst()
{
try{
this.rs.beforeFirst();
}catch(SQLExceptione){
System.out.print(e.toString());
}
}
publicvoidrs_close()
{
try{
this.rs.close();
}catch(SQLExceptione){
System.out.print(e.toString());
}
}
publicvoidrs_deleteRow()
{
try{
this.rs.deleteRow();
}catch(SQLExceptione){
System.out.print(e.toString());
}
}
publicbooleanrs_first()
{
try{
this.rs.first();
returntrue;
}catch(SQLExceptione){
System.out.print(e.toString());
returnfalse;
}
}
publicStringrs_getString(Stringcolumn)
{
try{
returnthis.rs.getString(column);
}catch(SQLExceptione){
System.out.println(e.toString());
returnnull;
}
}
//此办法用于猎取年夜段文本,
//将个中的回车换行交换为<br>
//输入到html页面
publicStringrs_getHtmlString(Stringcolumn)
{
try{
Stringstr1=this.rs.getString(column);
Stringstr2="";
Stringstr3="<br>";
returnthis.replaceAll(str1,str2,str3);
}catch(SQLExceptione){
System.out.println(e.toString());
returnnull;
}
}
//把str1字符串中的str2字符串交换为str3字符串
privatestaticStringreplaceAll(Stringstr1,Stringstr2,Stringstr3)
{
StringBufferstrBuf=newStringBuffer(str1);
intindex=0;
while(str1.indexOf(str2,index)!=-1)
{
index=str1.indexOf(str2,index);
strBuf.replace(str1.indexOf(str2,index),str1.indexOf(str2,index)+str2.length(),str3);
index=index+str3.length();
str1=strBuf.toString();
}
returnstrBuf.toString();
}
publicintrs_getInt(Stringcolumn)
{
try{
returnthis.rs.getInt(column);
}catch(SQLExceptione){
System.out.println(e.toString());
return-1;
}
}
publicintrs_getInt(intcolumn)
{
try{
returnthis.rs.getInt(column);
}catch(SQLExceptione){
System.out.println(e.toString());
return-1;
}
}
publicbooleanrs_next()
{
try{
returnthis.rs.next();
}catch(SQLExceptione){
System.out.println(e.toString());
returnfalse;
}
}
//判别了局会合是不是无数据
publicbooleanhasData()
{
try{
booleanhas_Data=this.rs.first();
this.rs.beforeFirst();
returnhas_Data;
}catch(SQLExceptione){
System.out.println(e.toString());
returnfalse;
}
}
publicbooleanrs_last()
{
try{
returnthis.rs.last();
}catch(SQLExceptione){
System.out.println(e.toString());
returnfalse;
}
}
publicbooleanrs_previous()
{
try{
returnthis.rs.previous();
}catch(Exceptione){
System.out.println(e.toString());
returnfalse;
}
}
//main办法,调试用
publicstaticvoidmain(Stringargs[])
{
try{
DBConnmyconn=newDBConn();
//myconn.setDBName("shopping");
//myconn.DBConn();
//myconn.execute("InsertIntotbl_test(id,name)values(10,shandaer)");
//myconn.execute("Updatetbl_testsetname=yyyyyyyyyyyywhereid=10");
//myconn.execute("Deletefromtbl_testwhereid=1");
ResultSetrs=myconn.executeQuery("select*fromtbl_userorderbyiddesclimit1");
//booleanhasData=myconn.hasData();
//System.out.println("hasdata:"+hasData);
//rs.first();
while(myconn.rs.next())
{
intid=myconn.rs_getInt("id")+1;
System.out.print(id);
System.out.println(myconn.rs_getInt("id")+myconn.rs_getString("name"));
//System.out.println(+myconn.rs_getHtmlString("name"));
//System.out.println(myconn.rs.getString("name")+myconn.rs_getInt(1));
}
}catch(Exceptione){
System.err.println(e.toString());
}
}
}
声明:由于利用的是MySQL数据库,以是必要MySQL数据库的驱动
下载后请将org包放至DBConn.java地点目次下
以确保该bean能一般运转
3、编写用户注册的bean:reg.java
//reg.java
//importrequiredclasses
importjava.sql.*;
publicclassreg
{
publicintnewID=0;
publicbooleanresult=false;
publicbooleanreg(Stringusername,Stringpassword,Stringconfirm,Stringemail)
{
try{
if(!this.checkUser(username))
returnfalse;
if(!this.checkPwd(password))
returnfalse;
if(!this.verifyPwd(password,confirm))
returnfalse;
if(!this.checkEmail(email))
returnfalse;
if(!this.userNotExit(username))
returnfalse;
this.getNewID();
this.result=this.register(username,password,confirm,email);
returnthis.result;
}catch(Exceptione){
System.out.println(e.toString());
returnfalse;
}
}//Endbooleanreg
publicbooleancheckUser(Stringuser)
{
try{
if(user.indexOf("")!=-1)
{
System.out.println("姓名中含有不法字符!");
returnfalse;
}else
returntrue;
}catch(Exceptione){
System.out.println(e.toString());
returnfalse;
}
}
publicbooleancheckPwd(Stringpwd)
{
try{
if(pwd.indexOf("")!=-1)
{
System.out.println("暗码中含有不法字符!");
returnfalse;
}else
returntrue;
}catch(Exceptione){
System.out.println(e.toString());
returnfalse;
}
}
publicbooleanverifyPwd(Stringpwd,Stringconfirm)
{
try{
if(!pwd.equals(confirm))
{
System.out.println("两次输出的暗码纷歧致!");
returnfalse;
}else
returntrue;
}catch(Exceptione){
System.out.println(e.toString());
returnfalse;
}
}
publicbooleancheckEmail(Stringemail)
{
try{
if(email.indexOf("")!=-1)
{
System.out.println("E-mail中含有不法字符!");
returnfalse;
}else
returntrue;
}catch(Exceptione){
System.out.println(e.toString());
returnfalse;
}
}
publicbooleanuserNotExit(Stringuser)
{
try{
DBConnuserDBConn=newDBConn();
userDBConn.executeQuery("select*fromtbl_userwherename="+user+"");
if(userDBConn.rs_next())
{
System.out.println("用户名已存在,请选择别的的用户名!");
returnfalse;
}else
returntrue;
}catch(Exceptione){
System.out.println(e.toString());
returnfalse;
}
}
publicintgetNewID()
{
try{
DBConnnewIDDBConn=newDBConn();
newIDDBConn.executeQuery("select*fromtbl_userorderbyiddesclimit1");
if(newIDDBConn.rs_next())
{
this.newID=newIDDBConn.rs_getInt("id")+1;
System.out.println(this.newID);
}else{
this.newID=1;
}
returnthis.newID;
}catch(Exceptione){
System.out.println(e.toString());
return-1;
}
}
publicintgetID()
{
returnthis.newID;
}
publicbooleanregister(Stringusername,Stringpassword,Stringconfirm,Stringemail)
{
try{
DBConnregDBConn=newDBConn();
StringstrSQL="insertintotbl_user(id,name,pwd,email)values("+this.newID+","+username+","+password+","+email+")";
regDBConn.execute(strSQL);
returntrue;
}catch(Exceptione){
System.out.println(e.toString());
returnfalse;
}
}
publicstaticvoidmain(Stringargs[])
{
try{
regnewreg=newreg();
System.out.println(newreg.reg("sssssssss","ssssss","ssssss","imagebear@163.com"));
DBConnmyconn=newDBConn();
myconn.executeQuery("select*fromtbl_user");
while(myconn.rs_next())
{
System.out.println(myconn.rs_getInt("id")+""+myconn.rs_getString("name")+""+myconn.rs_getString("pwd")+""+myconn.rs_getString("email"));
}
System.out.println(newreg.getID());
}catch(Exceptione){
System.err.println(e.toString());
}
}
};
申明:
1、该bean文件应和上文所述DBConn.class文件放于统一目次下
2、本例次要研讨注册的历程,个中的Email检测等办法其实不完美,若要使用请自行计划办法
4、编写用户上岸的Servlet:login.java
//login.java
//importrequiredclasses
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
importjava.sql.*;
//classlogin
publicclassloginextendsHttpServlet
{
publicvoiddoGet(HttpServletRequestreq,HttpServletResponseres)
throwsIOException,ServletException
{
Stringusername=req.getParameter("username");
Stringpassword=req.getParameter("password");
if(this.checklogin(username,password))
{
Cookiemylogin=newCookie("username",username);
mylogin.setVersion(1);
mylogin.setPath("/");
mylogin.setComment("Yourloginusername");
res.addCookie(mylogin);
}
//Cookie[]myCookies=req.getCookies();
//StringnameValue=this.getCookieValue(myCookies,"username","notfound");
//PrintWriterout=res.getWriter();
//out.println("username"+":"+nameValue);
//out.println("TestCookieSuccess!");
res.sendRedirect("/index.jsp");
}
publicvoiddoPost(HttpServletRequestreq,HttpServletResponseres)
throwsIOException,ServletException
{
doGet(req,res);
}
publicstaticStringgetCookieValue(Cookie[]cookies,StringcookieName,StringdefaultValue)
{
for(inti=0;i<cookies.length;i++){
Cookiecookie=cookies[i];
if(cookieName.equals(cookie.getName()))
return(cookie.getValue());
}
return(defaultValue);
}
publicbooleanchecklogin(Stringusername,Stringpassword)
{
try{
DBConnloginConn=newDBConn();
loginConn.executeQuery("select*fromtbl_userwherename="+username+"");
if(loginConn.rs_next())
{
System.out.println("Connectioncreated!");
if(loginConn.rs_getString("pwd").trim().equals(password))
{
System.out.println(loginConn.rs_getString("name"));
returntrue;
}
else
{
returnfalse;
}
}
System.out.println("TestLoginSuccess!");
returnfalse;
}catch(Exceptione){
System.out.println(e.toString());
returnfalse;
}
}
publicstaticvoidmain(Stringargs[])
{
loginmylogin=newlogin();
System.out.println(mylogin.checklogin("shandong","shandong"));
}
}
申明:
1、默许的jdk1.4中并没有servlet包,请至sun公司网页下载servlet.jar,放至jdk目次下的jrelib目次下,并在JCreator中设置jdk处增加servlet.jar包
2、本Servlet用于查验用户名和暗码,若准确则将用户名写进Cookie,完成后将以后页重定向到index.jsp页
5、编写检测用户是不是已上岸的bean:checkLogin.java
//checkLogin.java
//importrequiredclasses
importjava.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
//classcheckLogin
publicclasscheckLogin
{
publicStringusername="";
publicbooleancheck(HttpServletRequestreq,HttpServletResponseres)
throwsIOException,ServletException
{
StringcookieName="username";
Cookie[]myCookies=req.getCookies();
this.username=this.getCookieValue(myCookies,cookieName,"notfound");
PrintWriterout=res.getWriter();
if(this.username!=null)
{
//out.println("早上好,"+this.username+"!");
returntrue;
}else{
out.println("上岸失利!");
returnfalse;
}
}
publicStringgetUserName()
{
returnthis.username;
}
publicstaticStringgetCookieValue(Cookie[]cookies,StringcookieName,StringdefaultValue)
{
for(inti=0;i<cookies.length;i++){
Cookiecookie=cookies[i];
if(cookieName.equals(cookie.getName()))
return(cookie.getValue());
}
return(defaultValue);
}
}
申明:此bean检测cookie中的username,若不为空则申明已登录,反之申明没有登录。办法不敷完美,您能够自行扩大。
6、在JRun中创建shopping服务器
翻开JRunAdministrator,新建shopping服务器,这里端口为8101。
将上文所述一切编译后的class文件连同org包拷至JRun的shopping服务器地点目次中的classes文件夹下,路径为:
C:JRun4serversshoppingdefault-eardefault-warWEB-INFclasses
7、创建jsp文件
使用DW,在C:JRun4serversshoppingdefault-eardefault-war目次下新建以下的jsp文件:
index.jsp:
<%@pagecontentType="text/html;charset=gb2312"pageEncoding="gb2312"%>
<html>
<head>
<title>Shopping123</title>
<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
<linkhref="styles/shoppingstyle.CSS"rel="stylesheet"type="text/css">
</head>
<bodybgcolor="#FFFFFF"leftmargin="0"topmargin="0">
<jsp:useBeanid="checklogin"class="checkLogin"scope="page"/>
<%
booleanlogin=checklogin.check(request,response);
%>
<tablewidth="100%"height="100%"border="0"cellpadding="0"cellspacing="0">
<trbgcolor="#990000">
<tdheight="80"colspan="5"><tablewidth="100%"height="100%"border="0"cellpadding="0"cellspacing="0">
<tr>
<tdwidth="120"></td>
<tdclass="caption">Shopping123</td>
<tdwidth="200"></td>
</tr>
</table></td>
</tr>
<tr>
<tdwidth="200"align="center"valign="top"><tablewidth="100%"height="20"border="0"cellpadding="0"cellspacing="0">
<tr>
<td></td>
</tr>
</table>
<%
if(!login){
%>
<tablewidth="90%"border="0"align="center"cellpadding="0"cellspacing="1"bgcolor="#CCCCCC">
<formname="form1"method="post"action="/servlet/login">
<tralign="center"bgcolor="#CCCCCC">
<tdheight="30"colspan="2"class="deepred">卖场出口</td>
</tr>
<tr>
<tdwidth="50%"height="24"align="center"bgcolor="#FFFFFF">会员</td>
<tdalign="center"bgcolor="#FFFFFF"><inputname="username"type="text"id="username"size="10"></td>
</tr>
<tr>
<tdheight="24"align="center"bgcolor="#FFFFFF">暗码</td>
<tdalign="center"bgcolor="#FFFFFF"><inputname="password"type="text"id="password"size="10"></td>
</tr>
<tr>
<tdheight="24"align="center"bgcolor="#FFFFFF"><ahref="reg.jsp"target="_blank"class="red">注册</a></td>
<tdalign="center"bgcolor="#FFFFFF"><inputtype="submit"name="Submit"value="进进"></td>
</tr>
</form>
</table>
<%
}
else
{
out.println("您好,"+checklogin.getUserName()+"!");
}
%>
</td>
<tdwidth="1"valign="top"bgcolor="#CCCCCC"></td>
<tdwidth="400"></td>
<tdwidth="1"valign="top"bgcolor="#CCCCCC"></td>
<tdwidth="200"></td>
</tr>
<tralign="center"bgcolor="#990000">
<tdheight="60"colspan="5"class="white">copyright |
|