登录功能

注释:过程中的错误看5、6、7,(DBUtil类中,用户名密码填写自己的用户名和密码)。

1、首先进行Mysql数据库的建立

在mysql数据库中建立test数据库,新建表user,表中两个列,分别为name,userpassword。·如下图

 

jsp拿到javabean后要怎么用 jsp+servlet+javabean+dao_sql

  2、之后打开eclipse新建Demo的web应用。如下图

 

jsp拿到javabean后要怎么用 jsp+servlet+javabean+dao_sql_02

 

3、在Demo的 Java Resources的文件夹下新建4个包,包名分别为bean,dao,db,servlet

在bean包中新建Userbean类,内容如下:

package bean;
 
import java.util.Date;
 
public class Userbean
{
private String username;
private String userpassword;
 
 
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpassword() {
return userpassword;
}
public void setUserpassword(String userpassword) {
this.userpassword = userpassword;
}
 
 
public Userbean() {
 
}
 
public Userbean(String username,String userpassword) {
super();
 
this.username = username;
this.userpassword = userpassword;
 
}
 
 
 
}

在dao中新建Admindao类,内容如下:

package dao;
 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
 
import db.DBUtil;
 
import bean.Userbean;
 
 
 
 
public class Admindao
{
 
public Userbean checkLogin(String username, String password)//检测登录
{
Connection conn=DBUtil.getConn();//连接
Userbean userbean=null;
Statement state=null;
ResultSet rs=null;
try
{
state=conn.createStatement();
rs=state.executeQuery("select * from user where name='"+username+"'");//查询
if(rs.next())
{//查询成功
if(rs.getString("userpassword").equals(password))
{
 
userbean = new Userbean();
userbean.setUsername(rs.getString("name"));
userbean.setUserpassword(rs.getString("userpassword"));
 
}
 
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
DBUtil.close(rs, state, conn);
}
return userbean;//返回值
}
 
 
}

在db中新建DBUtil类,内容如下->注:用户名密码填写自己的用户名和密码):

package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class DBUtil
{
public static String db_url="jdbc:mysql://localhost:3306/test?uerUnicode=true&characterEncoding=UTF-8";//test是连接的数据库名
public static String db_user="root";//连接mysql的用户名,填写你自己的
public static String db_password="root";//连接mysql的密码,填写你自己的
public static Connection getConn()
{
Connection conn=null;
try
{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection(db_url,db_user,db_password);
}
catch(Exception e)
{
e.printStackTrace();
}
return conn;
}
public static void close(Statement state,Connection conn)//关闭
{
if(state!=null)//判断
{
try
{
state.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
if(conn!=null)
{
try
{
conn.close();
}
catch(SQLException e)
{
 e.printStackTrace();
}
}
}
public static void close(ResultSet rs,Statement state,Connection conn)
{
if(rs!=null)
{
try
{
rs.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
if(state!=null)
 
{
try
{
state.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
if(conn!=null)
{
try
{
conn.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
 
}

 在servlet包中新建Adminservlet类,内容如下:(出错误看后面的5须要加tomcat服务)

package servlet;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
import bean.Userbean;
import dao.Admindao;
 
 
 
 
@SuppressWarnings("serial")
public class Adminservlet extends HttpServlet
{
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
 
{
// TODO Auto-generated method stub
req.setCharacterEncoding("utf-8");
String method = req.getParameter("method");
 
if ("login".equals(method))
{
try {
login(req, resp);
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
 
}
 
 
 
 
 
private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ClassNotFoundException, SQLException
{
req.setCharacterEncoding("utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
Admindao adminDao = new Admindao();
Userbean userbean = adminDao.checkLogin(username, password);
if(userbean==null)
{
System.out.println("0");
req.getRequestDispatcher("/fail.jsp").forward(req, resp);
}
else
{
System.out.println("1");
req.getRequestDispatcher("/success.jsp").forward(req, resp);
 
}
 
 
 
}
 
 
}

3、在WebContent文件夹中新建login.jsp文件,内容如下:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>登录</title>
<script language="javascript">
function checkinfo(){
 
var username = document.getElementById('username');
var password = document.getElementById('password');
 
if(username.value ==""&&password.value =="")
{
document.getElementById('username').focus();//给用户名获取焦点
alert('请输入用户名和密码!!!');
}
else if(username.value =="")
{
document.getElementById('username').focus();//给用户名获取焦点
alert('请输入用户名!!!');
}
else if(password.value =="")
{
document.getElementById('password').focus();//给密码获取焦点
alert('请输入密码!!!');
}
else
{
document.getElementById("form").submit();
}
}
</script>
</head>
<body onload= "javascript:document.getElementById('username').focus();">
<form id="form" action="${pageContext.request.contextPath}/admin/adminServlet?method=login" method="post" >
<table width="252" border="1" align="center">
  <tr>
    <td width="72"><strong>用户名:</strong></td>
    <td width="164"><input id="username" name="username" type="text" /></td>
  </tr>
  <tr>
密码:</strong></td>
    <td><input id="password" name="password" type="password" /></td>
  </tr>
  <tr>
    <td> </td>
    <td><button type="button" name="login" onclick="checkinfo()"  >登录</button>
    </td>
  </tr>
</table>
</form>
</body>
</html>

新建success.jsp文件,内容如下:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>success</title>
</head>
<body>
登录成功!!!
<input type="submit" value="退出" onclick="javascript:window.location.href='${pageContext.request.contextPath}/login.jsp';" >
</body>
</html>

新建fail.jsp文件,内容如下:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>fail</title>
<script language="javascript">
function fanhui(){
alert('用户名或密码不正确!');
document.getElementById("form").submit();
}
</script>
</head>
<body onload= "fanhui();">
<form id="form" action="${pageContext.request.contextPath}/login.jsp" >
</form>
</body>
</html>

4、在WebContent文件夹的WEB-INF文件夹中,右击新建其他,新建web.xml文件,或者选择新建文件,在名字后加上.xml(名字别乱起,文件位置必须是此路径),内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" >
<servlet>
<servlet-name>Adminservlet</servlet-name>
<servlet-class>servlet.Adminservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Adminservlet</servlet-name>
<url-pattern>/admin/adminServlet</url-pattern>
</servlet-mapping>
</web-app>

5、错误修改,错误如下

 

jsp拿到javabean后要怎么用 jsp+servlet+javabean+dao_jsp拿到javabean后要怎么用_03

还有

 

jsp拿到javabean后要怎么用 jsp+servlet+javabean+dao_java_04

此错误是因为tomcat服务没有加载,解决方法:

(1)右击Demo项目,选择构建路径,点击配置构建路径,显示如下图:

 

jsp拿到javabean后要怎么用 jsp+servlet+javabean+dao_sql_05

(2)点击Add Library 之后,点击下一步,选择Server Runtime,之后选择自己的tomcat服务,点击完成,如下图:

 

jsp拿到javabean后要怎么用 jsp+servlet+javabean+dao_java_06

jsp拿到javabean后要怎么用 jsp+servlet+javabean+dao_sql_07

 

之后点击确定,

jsp拿到javabean后要怎么用 jsp+servlet+javabean+dao_java_08

 

6、还有一个是Mysql的jar须要加进去,须要连接Mysql数据库

(方法1)可以在WebContent文件夹的WEB-INF中的lib文件夹中加入mysql的jar,如下图:

 

jsp拿到javabean后要怎么用 jsp+servlet+javabean+dao_sql_09

 

(2)右击Demo项目,选择构建路径,点击配置构建路径,之后点击添加外部JAR,选择自己路径下的Mysql的jar,选择后点击确定,最后一步如下图:

 

jsp拿到javabean后要怎么用 jsp+servlet+javabean+dao_jsp拿到javabean后要怎么用_10

 

7、Mysql的服务必须启动,将cmd(命令提示符)用管理员身份运行,之后输入 net start mysql 点击回车(mysql是我的mysql服务的名字,请输入你自己的mysql服务的名字)。