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

2024-04-24 08:48

本文主要是介绍JSP+JavaBean+Servlet技术实现某网站用户注册和登录功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

【问题】 乱码问题

解决页面乱码:

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

这篇关于JSP+JavaBean+Servlet技术实现某网站用户注册和登录功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/931290

相关文章

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

Java使用jar命令配置服务器端口的完整指南

《Java使用jar命令配置服务器端口的完整指南》本文将详细介绍如何使用java-jar命令启动应用,并重点讲解如何配置服务器端口,同时提供一个实用的Web工具来简化这一过程,希望对大家有所帮助... 目录1. Java Jar文件简介1.1 什么是Jar文件1.2 创建可执行Jar文件2. 使用java

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

C#实现一键批量合并PDF文档

《C#实现一键批量合并PDF文档》这篇文章主要为大家详细介绍了如何使用C#实现一键批量合并PDF文档功能,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录前言效果展示功能实现1、添加文件2、文件分组(书签)3、定义页码范围4、自定义显示5、定义页面尺寸6、PDF批量合并7、其他方法

SpringBoot实现不同接口指定上传文件大小的具体步骤

《SpringBoot实现不同接口指定上传文件大小的具体步骤》:本文主要介绍在SpringBoot中通过自定义注解、AOP拦截和配置文件实现不同接口上传文件大小限制的方法,强调需设置全局阈值远大于... 目录一  springboot实现不同接口指定文件大小1.1 思路说明1.2 工程启动说明二 具体实施2