您的位置:首页 > 编程语言 > Java开发

jsp+servlet+javaBean简单登录实例

2013-04-23 08:43 513 查看
login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>管理员登录</title>
<script type="text/javascript">
function check(){
if(window.loginForm.name.value=="" || window.loginForm.name.value.length<=3){
alert("警告!用户不能名为空!长度不能小于3位!");
window.loginForm.name.focus();
}
else if(window.loginForm.pwd.value=="" || window.loginForm.pwd.value.length<6){
alert("警告!密码不能为空!密码长度不能小于6为位!");
window.loginForm.pwd.focus();
}
}
</script>
</head>
<body>
<form action="LoginServlet" name="loginForm" method="post">
用户名:<input type="text" name="name"><br>
密  码:<input type="password" name="pwd"><br>
<input type="submit" value="提交" onclick="check()">
<input type="reset" value="重填">
</form>
</body>
</html>


error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>error page</title>
</head>
<body>
<center>
<font color="red" size="30">用户名或密码错误!</font> <br>
<a href="login.jsp">重新登录</a>
</center>
</body>
</html>


index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
OK <br>
</body>
</html>


LoginBean.java

package admin.loginBean;

import java.sql.*;

public class LoginBean{
//定义属性,设置为私有
private String name;
private String pwd;
private Connection conn;
private PreparedStatement ps;
private ResultSet rs;

//定义uid 返回值为String类型的get方法
public String getName(){
return name;
}

//定义pwd返回值为String类型的get方法
public String getPwd(){
return pwd;
}

//定义uid返回值为空的set方法
public void setName(String name){
this.name = name;
}

//定义pwd返回值为空的set方法
public void setPwd(String pwd){
this.pwd = pwd;
}

//创建连接驱动
public int check(){
if(this.getName()==null) return -1;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:news");
String qstr="select * from login where name=? and pwd=?";
ps=conn.prepareStatement(qstr);
ps.setString(1,this.getName());
ps.setString(2,this.getPwd());
rs=ps.executeQuery();
if(rs.next()){
return 1;
}
else{
return 0;
}
}
catch (ClassNotFoundException ex){}
catch (SQLException ex1){}
finally{
try {
conn.close();
conn=null;
} catch (SQLException e) {
e.printStackTrace();
}
}
return -1;
}
}


LoginServlet.java

package admin.loginServlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import admin.loginBean.LoginBean;
public class LoginServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
private static final String CONTENT_TYPE = "text/html; charset=GBK";

public LoginServlet() {
super();
}
public void destroy() {
super.destroy();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet1</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<p>The servlet has received a " + request.getMethod() +
". This is the reply.</p>");
out.println("</body>");
out.println("</html>");
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//  PrintWriter out = response.getWriter();
LoginBean lb = new LoginBean();
HttpSession session=request.getSession();
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
lb.setName(name);
lb.setPwd(pwd);
lb.check();
if(lb.check()==1){
request.getRequestDispatcher("index.jsp").forward(request, response);
}else{
session.setAttribute("name", name);
request.getRequestDispatcher("error.jsp").forward(request, response);
}
}
public void init() throws ServletException { }
}


LoginFilter.java

package admin.loginFilter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
public class LoginFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;
//Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
//Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) {
try {
request.setCharacterEncoding("GBK");
filterChain.doFilter(request, response);
}
catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
}
catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
}
//Clean up resources
public void destroy() {
}
}


web.xml

<?xml version="1.0" encoding="GBK"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>WebRoot</display-name>
<filter>
<filter-name>LoginFilter</filter-name>
<filter-class>admin.loginFilter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoginFilter</filter-name>
<servlet-name>LoginServlet</servlet-name>
</filter-mapping>
<servlet>
<description>LoginServlet</description>
<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>admin.loginServlet.LoginServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
</web-app>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: