JSP+JavaBean+Servlet技术实现某网站用户注册和登录功能

时间:2022-11-12 21:06:38

【问题】 乱码问题

解决页面乱码:

response.setCharacterEncoding("utf-8");

与服务器连接中的乱码
String user1 = new String(request.getParameter("tuser").getBytes("ISO8859_1"),"utf-8");

【问题】小白问题

javabean 是从新建中弄得,但是得首先建package 

然后再在 右键相应的包新建 class

.class  文件自动生成,但是要删掉其中的某些部分

不太明白参考:http://blog.163.com/xiaohui_1123@126/blog/static/39805240200961201443112/

知识:

1:  PreparedStatement

http://www.knowsky.com/363097.html

http://baike.baidu.com/view/890310.htm

http://www.2cto.com/kf/201303/192838.html

源码:

xml 文件代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Dao</servlet-name>
    <servlet-class>Dao</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>loginServlet</servlet-name>
    <servlet-class>servlet.loginServlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>addUserServlet</servlet-name>
    <servlet-class>servlet.addUserServlet</servlet-class>
  </servlet>



  <servlet-mapping>
    <servlet-name>Dao</servlet-name>
    <url-pattern>/Dao</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>loginServlet</servlet-name>
    <url-pattern>/loginServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>addUserServlet</servlet-name>
    <url-pattern>/addUserServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 javabean 里的Java文件代码:
1:User.java
package bean;

public class User {
    private String user;
    private String pwd;
    private String lever;
    private String sex;
    private String time;
public String getUser() {
    return user;
}
public void setUser(String user) {
    this.user = user;
}

public String getPwd() {
    return pwd;
}
public void setPwd(String pwd) {
    this.pwd = pwd;
}

public String getSex() {
    return sex;
}
public void setSex(String sex) {
this.sex = sex;
}

public String getLever() {
    return lever;
}
public void setLever(String lever) {
    this.lever = lever;
}

public String getTime() {
    return time;
}
public void setTime(String time) {
    this.time = time;
}
}
2:Dao.java
package dao;
import getConnection.GetConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import bean.User;

public class Dao
{
    private Connection conn;
    private PreparedStatement pstat;
    String sql="";
/**
*
* 用户登录
*/
public boolean logoin(User user) throws SQLException
{
    conn = GetConnection.getConnection();
    boolean i = false;
    sql = "select * from product where tName=? and tPwd=?";
    pstat = conn.prepareStatement(sql);
    pstat.setString(1, user.getUser());
    pstat.setString(2, user.getPwd());

    ResultSet rs1 = (ResultSet) pstat.executeQuery();
    if (rs1.next())
    {
        i = true;
        rs1.close();
        pstat.close();
    }
    else
    {
        i = false ;
        rs1.close();
        pstat.close();
    }
    conn.close();
    return i;
}
/**
* 用户注册
*/
public void addUser(User user)
{
    conn = GetConnection.getConnection();
    sql = "insert into product values(?,?,?,?,?)";
try
{
    pstat = conn.prepareStatement(sql);
    pstat.setString(1,user.getUser());
    pstat.setString(2,user.getPwd());
    pstat.setString(3,user.getSex());
    pstat.setString(4,user.getLever());
    pstat.setString(5,user.getTime());
    pstat.executeUpdate();
    pstat.close();
    conn.close();
}catch(SQLException e)
{
    e.printStackTrace();
}

}
}
3:GetConnection.java
package getConnection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* 用JDBC的方法获得数据库的连接
*
*/
public class GetConnection {
//通过静态方法注册驱动,获得连接
public static Connection getConnection()
{
    String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=mystar";
    Connection con = null;
    try {
        Class.forName(driver);
        try {
            con = DriverManager.getConnection(url,"sa","sa");
            }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
    catch (ClassNotFoundException e)
    {
        e.printStackTrace();
    }
    System.out.println("已获得数据库的连接");
    return con;
}
/*public static void main(String args[]){
getConnection();
}*/
}
Servlet 文件中的代码:
1:登录servlet  loginServlet.java
package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bean.User;
import dao.Dao;

public class loginServlet extends HttpServlet {
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html");

PrintWriter out = response.getWriter();
String name = new String (request.getParameter("tuser").getBytes("ISO8859_1"), "utf-8");
String pwd = new String (request.getParameter("tpwd").getBytes("ISO8859_1"),"UTF-8");

User user = new User();
user.setUser(name);
user.setPwd(pwd);

Dao dao = new Dao();
boolean isLogin;
try
{
        isLogin = dao.logoin(user);

        if(isLogin)
        {
            response.sendRedirect("MyJsp.jsp");
        }else
        {
            response.sendRedirect("index.jsp");
        }
} catch (SQLException e)
{
    e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void init() throws ServletException {
}

}
2:注册的servlet  addUserServlet.java
package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.Dao;
import bean.User;

public class addUserServlet extends HttpServlet {

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
String user1 = new String(request.getParameter("tuser").getBytes("ISO8859_1"),"utf-8");
String pwd = new String(request.getParameter("tpwd").getBytes("ISO8859_1"),"utf-8");
String sex = new String(request.getParameter("tsex").getBytes("ISO8859_1"),"utf-8");
String lever = new String(request.getParameter("tpwd").getBytes("ISO8859_1"),"utf-8");
String time = new String(request.getParameter("ttime").getBytes("ISO8859_1"),"utf-8");
System.out.print(user1+"  "+pwd+"  "+sex+"  "+lever+"  "+time);
User user = new User();
user.setUser(user1);
user.setPwd(pwd);
user.setSex(sex);
user.setLever(lever);
user.setTime(time);
Dao dao = new Dao();

dao.addUser(user);
/* request.setAttribute("info",new String("<br><br><center><h1><font color=red>添加成功!恭喜!!" +
"</font></h1></center><br>"));
request.setAttribute("id", new String("a"));
request.setAttribute("denglu",new String("<br><br><center><a href = /jspDev/index.jsp target =_parent>登陆</href></center><br>"));*/


request.getRequestDispatcher("info.jsp").forward(request,response);
}

public void init() throws ServletException {
// Put your code here
}

}
jsp 中的代码:
1:index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>欢迎来到学生管理系统</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>
<div align="center"> <font size="+2" color="#FF6633">用户登录</font>
</div>
<form id="form1" name="form1" method="post" action="loginServlet">
<table width="357" border="0" align="center">
<tr>
<td width="128">用户名:</td>
<td width="219"><label>
<input name="tuser" type="text" id="tuser" />
</label></td>
</tr>
<tr>
<td>密 码:</td>
<td><label>
<input name="tpwd" type="password" id="tpwd" />
</label></td>
</tr>
<tr>
<td><label>
<input type="submit" name="Submit" value="登录" />
</label></td>
<td><label><a href="addUser.jsp">用户注册</a></label></td>
</tr>
</table>
<p>&nbsp;</p>
</form>
</body>
</html>
2:addUserServlet.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>欢迎来到学生管理系统</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>
<div align="center"> <font size="+2" color="#FF6633">用户登录</font>
</div>
<form id="form1" name="form1" method="post" action="loginServlet">
<table width="357" border="0" align="center">
<tr>
<td width="128">用户名:</td>
<td width="219"><label>
<input name="tuser" type="text" id="tuser" />
</label></td>
</tr>
<tr>
<td>密 码:</td>
<td><label>
<input name="tpwd" type="password" id="tpwd" />
</label></td>
</tr>
<tr>
<td><label>
<input type="submit" name="Submit" value="登录" />
</label></td>
<td><label><a href="addUser.jsp">用户注册</a></label></td>
</tr>
</table>
<p>&nbsp;</p>
</form>
</body>
</html>
info.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'info.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>
   恭喜你,注册成功 <br>
  </body>
</html>
Myjsp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'MyJsp.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>
    恭喜你,登录成功<br>
  </body>
</html>
感谢:源码:(数据库不同,有些xiaobugs)

http://www.javaweb.cc/other/code/25236.shtml