登陸例項(JSP+Servlet+JavaBean)
阿新 • • 發佈:2018-12-23
/**************************************************
*Author:Java619
*Time:2006-03-25
*E-Mail:[email protected]
*QQ:395676370
**************************************************/
登陸是我們在實際應用中經常用到的,我在這邊舉個簡單的登陸例子,作為拋磚引玉吧!
程式結構如下:
採用JSP+Servlet+JavaBean
1.資料庫結構(為簡便這邊採用access,實際應用中建議採用其他資料庫如MySQL,MSSQL等)
==============================
uname 使用者名稱 文字型
pword 密碼 文字型
初始資料uname :ceun pword :123
==============================
2.檢視(JSP)
<%@page contentType="text/html"%><%@page pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content
<title>登陸</title>
</head>
<body>
<center><br><br><br>
<p><form action="<%=request.getContextPath ()%>/CheckServlet" method="post">
<table width="259" border="1" cellpadding="0" cellspacing="0" bordercolor
<tr align="center">
<td height="20" colspan="2"><span class="style1">登陸</span></td>
</tr>
<tr>
<td width="50" height="20">使用者名稱</td>
<td width="161" align="left"><input name="uname" type="text" id="uname" size="19"></td>
</tr>
<tr>
<td height="20">密碼</td>
<td align="left"><input name="pword" type="password" id="pword" size="20"></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="Submit" value="提交">
<input type="reset" name="Submit" value="重置"></td>
</tr>
</table></form></p>
</center>
</body>
</html>
3.Servlet
/** CheckServlet.java
*@author Java619
* Created on 2006年3月25日, 下午6:05
*/
package com.ceun;
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.ceun.bean.UserBean;
/**
*
* @author ceun
* @version
*/
publicclass CheckServlet extends HttpServlet {
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protectedvoid processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name=request.getParameter("uname");
String pword=request.getParameter("pword");
out.println("<br><br><br><hr><center><font color=red size=12><B>");
try{
UserBean user=new UserBean();
if(user.check(name,pword))
out.println("登陸成功");
else
out.println("登陸失敗");
}catch(Exception e){
}
out.println("</B></font></center>");
out.close();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protectedvoid doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return"Short description";
}
// </editor-fold>
}
4.JavaBean
/** UserBean.java
*@author Java619
* Created on 2006年3月25日, 下午6:22
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.ceun.bean;
import java.sql.*;
import com.ceun.util.DbUtil;
/**
*
* @author ceun
*/
publicclass UserBean {
/** Creates a new instance of UserBean */
public UserBean() {
}
public boolean check(String username,String password)
throws Exception{
Connection con= DbUtil.connectToDb();
Statement stmt=null;
try{
String sql="SELECT * FROM loginInfo "+
" WHERE uname='"+username+"' and pword='"+password+"'";
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()) returntrue;
}
catch(Exception ex)
{
}finally{
try{
stmt.close();
con.close();
}catch(Exception e){e.printStackTrace();}
}
returnfalse;
}
}
5.實用類(用於連線資料庫)
/** DbUtil.java
*
* Created on 2006年3月25日, 下午6:20
*@author Java619
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.ceun.util;
import java.io.*;
import java.sql.*;
/**
* <strong>DbUtil</strong> is a utility class to create
* a connection to our sample database.
*/
publicclass D