資源簡介
這個文件包,包括Register與TestLogin,Register用戶登錄界面,講注冊的信息存入到數據庫,TestLogin即登錄界面檢查密碼是否與數據庫一致,一致的話進入,并且在主頁面可以顯示用戶登錄信息,這里有個實驗報告作參考,代碼自己體會

代碼片段和文件信息
package?zhaoli;
import?java.io.IOException;
import?java.io.PrintWriter;
import?java.sql.Connection;
import?java.sql.DriverManager;
import?java.sql.SQLException;
import?java.sql.Statement;
import?javax.servlet.ServletException;
import?javax.servlet.http.HttpServlet;
import?javax.servlet.http.HttpServletRequest;
import?javax.servlet.http.HttpServletResponse;
public?class?RegisterServlet?extends?HttpServlet?{
/**
?*?Constructor?of?the?object.
?*/
public?RegisterServlet()?{
super();
}
/**
?*?Destruction?of?the?servlet.?
?*/
public?void?destroy()?{
super.destroy();?//?Just?puts?“destroy“?string?in?log
//?Put?your?code?here
}
/**
?*?The?doGet?method?of?the?servlet.?
?*
?*?This?method?is?called?when?a?form?has?its?tag?value?method?equals?to?get.
?*?
?*?@param?request?the?request?send?by?the?client?to?the?server
?*?@param?response?the?response?send?by?the?server?to?the?client
?*?@throws?ServletException?if?an?error?occurred
?*?@throws?IOException?if?an?error?occurred
?*/
public?void?doGet(HttpServletRequest?request?HttpServletResponse?response)
throws?ServletException?IOException?{
doPost(requestresponse);
}
/**
?*?The?doPost?method?of?the?servlet.?
?*
?*?This?method?is?called?when?a?form?has?its?tag?value?method?equals?to?post.
?*?
?*?@param?request?the?request?send?by?the?client?to?the?server
?*?@param?response?the?response?send?by?the?server?to?the?client
?*?@throws?ServletException?if?an?error?occurred
?*?@throws?IOException?if?an?error?occurred
?*/
public?void?doPost(HttpServletRequest?request?HttpServletResponse?response)
throws?ServletException?IOException?{
Connection?cn?=?null;
Statement?st?=?null;
String?url?=?“jdbc:sqlserver://localhost;database=Users;User=sa;password=mis_123“;
try?{
cn?=?DriverManager.getConnection(url);
st?=?cn.createStatement();
String?userName?=?new?String(request.getParameter(“username“).getBytes(“iso-8859-1“)“utf-8“);
String?password?=?new?String(request.getParameter(“password“).getBytes(“iso-8859-1“)“utf-8“);
String?realname?=?new?String(request.getParameter(“realname“).getBytes(“iso-8859-1“)“utf-8“);
String?gender?=?new?String(request.getParameter(“gender“).getBytes(“iso-8859-1“)“utf-8“);
String?email?=?new?String(request.getParameter(“email“).getBytes(“iso-8859-1“)“utf-8“);
String?sql?=?“insert?into?Users?values(‘“?
+?userName?+?“‘‘“?+?password?+?“‘‘“?+?realname?+“‘‘“?+?gender?+“‘‘“?
+?email?+“‘)“;
st.executeUpdate(sql);
System.out.println(“OK“);
}?catch?(SQLException?e)?{
System.out.println(e.getMessage());
}
finally{
if?(st!=null) try?{st.close();}?catch?(SQLException?e)?{}
if?(cn!=null) try?{cn.close();}?catch?(SQLException?e)?{}
}
response.setContentType(“text/html“);
PrintWriter?out?=?response.getWriter();
out.println(“
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????.......???????599??2011-04-14?20:33??用戶登錄\Register\.classpath
?????文件????????297??2011-04-19?15:16??用戶登錄\Register\.myme
????.......??????1751??2011-04-14?19:51??用戶登錄\Register\.project
????.......???????500??2010-03-29?09:18??用戶登錄\Register\.settings\.jsdtscope
????.......???????395??2011-04-14?19:51??用戶登錄\Register\.settings\org.eclipse.jdt.core.prefs
????.......???????462??2011-04-14?19:51??用戶登錄\Register\.settings\org.eclipse.wst.common.component
????.......???????252??2011-04-14?19:51??用戶登錄\Register\.settings\org.eclipse.wst.common.project.facet.core.xm
????.......????????49??2011-04-14?19:51??用戶登錄\Register\.settings\org.eclipse.wst.jsdt.ui.superType.container
????.......?????????6??2011-04-14?19:51??用戶登錄\Register\.settings\org.eclipse.wst.jsdt.ui.superType.name
????.......??????3536??2011-04-14?21:21??用戶登錄\Register\src\zhaoli\RegisterServlet.java
????.......???????834??2010-03-29?09:18??用戶登錄\Register\WebRoot\index.jsp
????.......????????36??2010-03-29?09:18??用戶登錄\Register\WebRoot\me
????.......??????1356??2011-03-28?11:15??用戶登錄\Register\WebRoot\MyHtml.html
?????文件???????3419??2011-04-19?15:16??用戶登錄\Register\WebRoot\WEB-INF\classes\zhaoli\RegisterServlet.class
????.......???????829??2011-03-28?10:38??用戶登錄\Register\WebRoot\WEB-INF\web.xm
????.......???????599??2011-04-14?14:21??用戶登錄\TestLogin\.classpath
?????文件????????300??2011-04-19?15:16??用戶登錄\TestLogin\.myme
????.......??????1752??2011-03-31?10:17??用戶登錄\TestLogin\.project
????.......???????493??2011-03-31?09:34??用戶登錄\TestLogin\.settings\.jsdtscope
????.......???????395??2011-03-31?09:34??用戶登錄\TestLogin\.settings\org.eclipse.jdt.core.prefs
????.......???????465??2011-03-31?09:34??用戶登錄\TestLogin\.settings\org.eclipse.wst.common.component
????.......???????252??2011-03-31?09:35??用戶登錄\TestLogin\.settings\org.eclipse.wst.common.project.facet.core.xm
????.......????????49??2011-03-31?09:35??用戶登錄\TestLogin\.settings\org.eclipse.wst.jsdt.ui.superType.container
????.......?????????6??2011-03-31?09:35??用戶登錄\TestLogin\.settings\org.eclipse.wst.jsdt.ui.superType.name
?????文件???????2521??2011-04-19?16:19??用戶登錄\TestLogin\src\com\song\IdentityServlet.java
????.......??????3297??2011-04-17?14:05??用戶登錄\TestLogin\src\com\song\LoginServlet.java
????.......??????1868??2011-04-17?14:05??用戶登錄\TestLogin\src\com\song\LogoutServlet.java
?????文件???????2837??2011-04-19?15:50??用戶登錄\TestLogin\src\com\song\MainServlet.java
?????文件???????3738??2011-04-19?15:48??用戶登錄\TestLogin\src\com\song\ShowServlet.java
????.......???????848??2011-03-31?09:53??用戶登錄\TestLogin\WebRoot\error.jsp
............此處省略33個文件信息
評論
共有 條評論