登录注册 (java jdbc servlet jsp)
来源:互联网 发布:activemq amqp java 编辑:程序博客网 时间:2024/05/17 23:11
这是最基础的java jsp servlet jdbc内容的混合使用,主要实现了jsp页面的登录注册功能,同时连接数据库。
1、创建实体化对象:
package com.cn.info;public class Info { private String id; private String name; private String password;// private String password1; private String sex; private int age; private String tel; private String adress; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getAdress() { return adress; } public void setAdress(String adress) { this.adress = adress; }}
2、编写相关的jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>登录</title> <body bgcolor = "blue"> <h1 align = center >欢迎您的到来</h1> <form action = "LoginServlet" method = "post"> <table border = "1" align = center> <tr> <td>i d:</td> <td><input type = "text" name = "id" ></td> </tr> <tr> <td>密 码:</td> <td><input type = "password" name = "password" ></td> </tr> <tr> <td> <th> <input type = "submit" value = "登录"> <input type = "reset" value = "重置"> <input type = "button" value = "注册" onClick="window.location.href='register.jsp'"></th> </td> </tr> </table></form> </body></html>
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>注册成功</title> </head> <body> <h1 align = "center">注册成功!</h1> <h1 align = "center">请返回登录页面----------</h1> </body></html>
2、创建登录注册的servletpackage com.cn.servlet;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 com.cn.add.AddUser;import com.cn.info.Info;@SuppressWarnings("serial")public class AddUserServlet extends HttpServlet { /** */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); request.setCharacterEncoding("gb2312"); response.setCharacterEncoding("gb2312"); PrintWriter out = response.getWriter(); String id = request.getParameter("id"); String name = request.getParameter("name"); String password = request.getParameter("password"); String sex = request.getParameter("sex"); int age = Integer.parseInt(request.getParameter("age")); String tel = request.getParameter("tel"); String adress = request.getParameter("adress"); Info info = new Info(); info.setId(id); info.setName(name); info.setPassword(password); info.setSex(sex); info.setAge(age); info.setAdress(adress); info.setTel(tel); AddUser adduser = new AddUser(); adduser.add(info); request.getRequestDispatcher("registersuccess.jsp").forward(request, response); out.flush(); out.close(); } /** * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here }}
package com.cn.servlet;
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 com.cn.databases.Select;
import com.cn.info.Info;
public class LoginServlet extends HttpServlet {
/** * Destruction of the servlet. <br> */public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here}/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); request.setCharacterEncoding("gb2312"); response.setCharacterEncoding("gb2312"); PrintWriter out = response.getWriter(); String id = request.getParameter("id"); String password= request.getParameter("password"); Info info = new Info(); info.setId(id); info.setPassword(password); Select s = new Select(); s.login(info); request.getRequestDispatcher("successful.jsp").forward(request,response); out.flush(); out.close();}/** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */public void init() throws ServletException { // Put your code here}
}
3、创建数据库连接
package com.cn.databases;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import com.mysql.jdbc.Connection;public class Jdbc_Connection { static String DriverName = "com.mysql.jdbc.Driver"; static String url = "jdbc:mysql://localhost:3306/homework?useUnicode=true&characterEncoding=utf8"; static String username = "root"; static String pswd = "123456"; static { try{ Class.forName(DriverName); System.out.println("创建连接成功!"); }catch(ClassNotFoundException e){ System.out.println("创建驱动失败!"); e.printStackTrace(); } } public static Connection getConnection(){ Connection con = null; try{ con = (Connection) DriverManager.getConnection(url, username, pswd); System.out.println("数据库连接成功!"); }catch(SQLException e){ System.out.println("数据库连接失败!"); e.printStackTrace(); } return con; } public static void free(ResultSet rs, Connection con,Statement stmt){ try{ if(rs != null) rs.close(); }catch(SQLException e){ System.out.println("关闭ResultSet失败!"); e.printStackTrace(); }finally{ try{ if(con != null) con.close(); }catch(SQLException e){ System.out.println("关闭Connection失败"); e.printStackTrace(); }finally{ try{ if(stmt != null) stmt.close(); }catch(SQLException e){ System.out.println("关闭Statement失败"); e.printStackTrace(); } } } } //public static void main(String[] agrs){// Jdbc_Connection j = new Jdbc_Connection(); // Jdbc_Connection.getConnection(); }}
4、登录相当于在数据库中查找并且匹配判断,注册相当于添加数据到数据库这里写代码片
package com.cn.databases;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.cn.info.Info;
import com.mysql.jdbc.Connection;
public class Select {
public void login(Info info){
Connection con = null;
PreparedStatement psmt = null;
ResultSet rst = null;
try{
con = Jdbc_Connection.getConnection();
String sql = “Select * from login where id= ?”;
psmt = con.prepareStatement(sql);
psmt.setString(1,info.getId());
rst = psmt.executeQuery();
if(rst.next()){
// String password = rst.getString(“password”);
// System.out.println(password);
if(rst.getString(“password”).equals(info.getPassword())){
System.out.println(“登陆成功!”);
}else{
System.out.println(“登录失败!”);
}
}else{
System.out.println(“没有该用户请返回注册!”);
}
}catch(Exception e){
e.printStackTrace();
}finally{
Jdbc_Connection.free(rst, con,psmt);
}
}
}
“`
package com.cn.add;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.cn.databases.Jdbc_Connection;
import com.cn.info.Info;
import com.mysql.jdbc.Connection;
public class AddUser {
public void add(Info info ){
Connection con = null;
PreparedStatement psmt = null;
ResultSet rst = null;
try{
con=Jdbc_Connection.getConnection();
String sql = "insert into login values(?,?,?,?,?,?,?)"; psmt = con.prepareStatement(sql); psmt.setString(1,info.getId() ); psmt.setString(2, info.getPassword()); psmt.setString(3, info.getName()); psmt.setString(4,info.getSex()); psmt.setInt(5, info.getAge()); psmt.setString(6, info.getTel()); psmt.setString(7, info.getAdress()); psmt.executeUpdate(); System.out.println("注册成功!"); }catch(Exception e){ e.printStackTrace(); }finally{ Jdbc_Connection.free(rst, con, psmt); }}
}
5、创建处理编码异常的过滤器
package com.cn.filter;
import java.io.IOException;
import java.nio.file.DirectoryStream.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
@SuppressWarnings(“rawtypes”)
public abstract class ChineseFilter implements Filter{
public void destroy(){
}public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException{request.setCharacterEncoding("utf-8");//将所有request内的对象设置字符集为utf-8response.setCharacterEncoding("utf-8");//将所有response内的对象设置字符集为utf-8chain.doFilter(request,response);//用chain 的doFilter处理过滤}public void init(FilterConfig arg0) throws ServletException{}
}
无法实现登录注册失败的跳转页面,想不出怎么实现,求支招
- 登录注册 (java jdbc servlet jsp)
- Jsp+Servlet+JDBC 登录
- JDBC+Servlet+JSP+Mysql写的一个简单的登录注册
- 用户注册登录测试版(jsp+servlet+mysql)
- 用户注册登录退出(jsp+servlet+sql2008)
- 纯JSP+JDBC+MySQL生成登录注册
- 登录和注册(jsp+servlet+JavaBean)
- JSP+Servlet实现注册登录功能
- jsp+Servlet+javaBean实现登录和注册
- JSP+Servlet实现注册登录功能
- Servlet +JSP+Javabean 实现用户登录注册
- servlet+jsp+jdbc简单登录系统
- java web学习总结22:基于Servlet+JSP+JavaBean开发模式的用户登录注册
- 使用jsp+servlet+jdbc做的登陆注册案例
- JSP+Servlet+JavaBean传统方式实现简易留言板制作(注册、登录、留言)
- JSP+JavaBean+Servlet用户注册登录请求的处理
- 简单的JSP-Servlet登录,注册,修改密码操作
- 完整的JSP+Servlet+MyEclipse编写的注册登录
- API函数之GetUserName
- mysql binlog日志及mysqlbinlog操作详解
- Spring对外部属性文件指定的某个属性进行加密、解密
- 设计模式
- HDU-2544 最短路 【最短路裸题】
- 登录注册 (java jdbc servlet jsp)
- Maven 中央存储库
- vpn 工作原理
- Blog搬家
- Android 自定义View 带你飞(二)
- 判断WPF中gridview中的数据是否为空,若为空则不显示该字段方法
- UINavigationController, UINavigationBar, UINavigationItem, navigationbar刷新
- 奇偶校验
- peerconnection_server目录中html代码简单注释与分析