燃烧的梦 2014-01-08
javajsp+servlet实现登录网页设计
这次涉及到四个文件:
登录页面:login.jsp
登录成功欢迎页面:success.jsp
登录失败页面:fail.jsp
Servlet处理文件:LoginServlet.java
其实还涉及到一个文件:web.xml,这个后面再说:
下面分别介绍这几个文件:
//--------------------------------------------------------------------------------------------------------------------
LoginServlet.java内容:
packageorg.cai.servlet;
importjava.sql.*;
importjava.io.*;
importjavax.servlet.http.*;
importjavax.servlet.*;
publicclassLoginServletextendsHttpServletimplementsServlet{
publicLoginServlet(){
}
protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
}
//protectedvoidservice(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
//System.out.println("-------------------------");
//doPost(request,response);
//}
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
response.setContentType("text/html;charset=gb2312");
request.setCharacterEncoding("gb2312");
Stringresult="";
//获取用户名
Stringusername=request.getParameter("username");
Stringpsw=request.getParameter("password");
if((username=="")||(username==null)||(username.length()>20)){
try{
result="请输入用户名(不能超过20个字符)!";
request.setAttribute("message",result);
response.sendRedirect("login.jsp");
}catch(Exceptione){
e.printStackTrace();
}
}
if((psw=="")||(psw==null)||(psw.length()>20)){
try{
result="请输入密码(不能超过20个字符)!";
request.setAttribute("message",result);
response.sendRedirect("login.jsp");
}catch(Exceptione){
e.printStackTrace();
}
}
//登记JDBC驱动程序
try{
Class.forName("org.gjt.mm.mysql.Driver");
}catch(ClassNotFoundExceptione){
e.printStackTrace();
System.out.println("ClassNotFoundException.");
}
//连接URL
Stringurl="jdbc:mysql://localhost:3306/learnJSP";
Connectionconn=null;
Statementstmt=null;
ResultSetrs=null;
try{
conn=DriverManager.getConnection(url,"root","root");
stmt=conn.createStatement();
//SQL语句
Stringsql="select*fromuserInfowhereusername='"+username+"'anduserpsw='"+psw+"'";
rs=stmt.executeQuery(sql);//返回查询结果
}catch(SQLExceptione){
e.printStackTrace();
}
HttpSessionsession=request.getSession();
session.setAttribute("username",username);
//System.out.println("+++++++++++++++++++++++"+username);
try{
if(rs.next()){//如果记录集非空,表明有匹配的用户名和密码,登陆成功
//登录成功后将username设置为session变量的username
//这样在后面就可以通过session.getAttribute("username")来获取用户名,
//同时这样还可以作为用户登录与否的判断依据
session.setAttribute("age",rs.getString("age"));
session.setAttribute("sex",rs.getString("sex"));
session.setAttribute("weight",rs.getString("weight"));
response.sendRedirect("success.jsp");
}else{
session.setAttribute("message","用户名或密码不匹配。");
response.sendRedirect("fail.jsp");
}
}catch(SQLExceptione){
e.printStackTrace();
}
}
privatestaticfinallongserialVersionUID=1L;
}
//--------------------------------------------------------------------------------------------------------------------
login.jsp内容:
<%@pagelanguage="java"contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>登陆</title>
</head>
<body>
<formmethod="POST"name="frmLogin"action="LoginServlet">
<h1align="center">用户登录</h1><br/>
<center>
<tableborder=1>
<tr>
<td>用户名:</td>
<td>
<inputtype="text"name="username"value="Yourname"size="20"maxlength="20"onfocus="if(this.value=='Yourname')this.value='';"/>
</td>
</tr>
<tr>
<td>密 码:</td>
<td>
<inputtype="password"name="password"value="Yourpassword"size="20"maxlength="20"onfocus="if(this.value=='Yourpassword')this.value='';"/>
</td>
</tr>
<tr>
<td>
<inputtype="submit"name="Submit"value="提交"onClick="returnvalidateLogin()"/>
</td>
<td>
<inputtype="reset"name="Reset"value="重置"/>
</td>
</tr>
</table>
</center>
</form>
<scriptlanguage="javascript">
functionvalidateLogin(){
varsUserName=document.frmLogin.username.value;
varsPassword=document.frmLogin.password.value;
if((sUserName=="")||(sUserName=="Yourname")){
alert("请输入用户名!");
returnfalse;
}
if((sPassword=="")||(sPassword=="Yourpassword")){
alert("请输入密码!");
returnfalse;
}
}
</script>
</body>
</html>
//--------------------------------------------------------------------------------------------------------------------
success.jsp内容;
<%@pagelanguage="java"contentType="text/html;charset=gb2312"pageEncoding="gb2312"%>
<html>
<head>
<title>欢迎</title>
</head>
<body>
<%
StringuserName=(String)session.getAttribute("username");
Stringage=(String)session.getAttribute("age");
Stringweight=(String)session.getAttribute("weight");
Stringsex=(String)session.getAttribute("sex");
System.out.println("性别:A"+sex+"A");
if(sex.trim().equals("M")){
sex="男";
}else{
sex="女";
}
%>
<divalign="center">
<%=userName%>
欢迎您,登陆成功!<br/>
<fontcolor="blue">登陆用户信息:</font>
<tableborder=1>
<tr>
<td> 姓名: </td>
<td> <%=userName%> </td>
</tr>
<tr>
<td> 年龄: </td>
<td> <%=age%> </td>
</tr>
<tr>
<td> 体重: </td>
<td> <%=weight%> kg </>
</tr>
<tr>
<td> 性别: </td>
<td> <%=sex%> </>
</tr>
</table>
<ahref="login.jsp">返回</a>
</div>
</body>
</html>
//--------------------------------------------------------------------------------------------------------------------
fail.jsp内容:
<%@pagelanguage="java"contentType="text/html;charset=gb2312"pageEncoding="gb2312"%>
<html>
<head>
<title>登陆失败</title>
</head>
<body>
<%
StringuserName=(String)session.getAttribute("username");
Stringmsg=(String)session.getAttribute("message");
%>
<divalign="center">
<%=userName%>
对不起,登陆失败!<br/>
<fontcolor="red">原因:</font>
<%=msg%>
<br/>
<br/>
5秒后将返回登陆界面。
</div>
<%
response.setHeader("Refresh","5;URL=/learnJSP/login.jsp");
%>
</body>
</html>
//--------------------------------------------------------------------------------------------------------------------
mysql数据库中需要创建数据库learnJSP,其中创建表userInfo创建表语句为:
createdatabaselearnJSP;
uselearnJSP
createtableuserInfo(idintnotnullprimarykey,usernamechar(20)notnull,userpswchar(20),ageint,weightint,sexenum('M','F'));
需要把连接mysql的jar包放到项目的lib目录中
//--------------------------------------------------------------------------------------------------------------------
web.xml内容:
<?xmlversion="1.0"encoding="ISO-8859-1"?>
<!DOCTYPEweb-app
PUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>
LoginServlet
</servlet-name>
<servlet-class>
org.cai.servlet.LoginServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>