当前位置: 首页 > >

javaWeb 简单注册登录(含数据库连接) -

发布时间:

这篇讲登录处理


传进来什么

user, pass



判什么
    用户名是否存在基于1中已存在的情况下判断密码是否匹配


响应页面

login.jsp:
1. 当登录成功, 写一个新页面, 并显示该用户的Sno
2. 失败则在登录页面基础上 + 错误提示


实现

新建一个名为LoginServlet的servlet
代码;


package adrui;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("Login Test");
String user = request.getParameter("user");
String pass = request.getParameter("pass");
Connection conn = (Connection)getServletContext().getAttribute("conn");
LoginModel obj = new LoginModel();
//第三方类用来连接数据库并判断登录结果
try{
String res = obj.check(conn, user, pass);
request.setAttribute("res", res);
RequestDispatcher view = request.getRequestDispatcher("login.jsp");//分派请求
view.forward(request, response);
}
catch(Exception e){
e.printStackTrace();
}
}

}


web.xml中配置:



login
adrui.LoginServlet


login
/login.do


LoginModel.java


package adrui;
import java.sql.*;
public class LoginModel {
private Statement state = null;
private String sql = "select * from user";
private ResultSet rs = null;
public String check(Connection conn, String user, String pass) throws SQLException{
state = conn.createStatement();
rs = state.executeQuery(sql);//查询
while(rs.next()){
if(rs.getString(2).equals(user)){
if(rs.getString(3).equals(pass)){
String sno = rs.getString(1);
return "1" + sno;//成功登录,后半段是Sno
}
return "-1";//密码不匹配
}
}
return "0";//用户名不存在
}
}


login.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>






Login Page





<% String f = (String)request.getAttribute("res");
if(f.substring(0, 1).equals("1")){

%>

Hello, <%= ((String)request.getAttribute("res")).substring(1) %>


<% }else{
%>



用户登录



USER:



PASS:









还没有账号? 立即注册




The time:




<% if(f.equals("0")){
%>

用户名错误


<%}else{ %>

密码错误


<%}} %>





测试

数据:






2.



3.



web.xml



LoginProject

login.html


register
adrui.registerServlet


register
/register.do


login
adrui.LoginServlet


login
/login.do


driver
com.mysql.jdbc.Driver


url
jdbc:mysql://localhost:3306/adrui?useSSL=true


user
root


pass
password



adrui.MyContextListener




最后说点

这个项目是在eclipse中写的, 如果想要放在本地tomcat服务器的webapps目录下, 浏览器键入localhost:8080/LoginProject运行, 要找对项目文件
项目文件在项目所在工作空间的
.metadata.pluginsorg.eclipse.wst.server.core mp0wtpwebapps目录下


总结
    jsp的基础还较薄弱, 这块用了不少时间细节错误太多, 特别是前端的大小写问题对于tomcat配置项目的了解还不够, 单eclipse项目导出就走了不少弯路姑且算是一个项目吧, 好歹用个几天时间呢


以上.



友情链接: