jsp用户登录注册

实现功能
登陆成功跳转到登陆成功界面
注册成功跳转到用户信息页面
登录界面login.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 'index.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>
		<form action="loginServlet" method="post">
			用户名:
			<input type="text" name="userName" />
			<br />
			密码:
			<input type="text" name="pwd" />${msg}
			<br />
			<input type="submit" value="登录" />
		</form>
	</body>
</html>

注册界面reg.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 'index.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>
		<form action="regServlet" method="post">
			用户名:
			<input type="text" name="userName" />
			<br />
			密码:
			<input type="text" name="pwd" />
<br />
			年龄:
			<input type="text" name="age" />
			<br />
			性别:
			<input type="text" name="sex" />
			<br />
			<input type="submit" value="注册" />
		</form>
	</body>
</html>

登陆成功界面success.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 'index.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>
     登陆成功!
  </body>
</html>

用户信息界面userList代码
第一种

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.zhangsan.bean.User"%>
<%
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 'userList.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>
  <table>
  
  <thead>
  <tr><th>编号</th><th>用户名</th><th>密码</th><th>性别</th><th>年龄</th></tr>
  </thead>
  
  <tbody>
  <%
  //ArrayList<User> users=(ArrayList<User>)request.getAttribute("users");
 // for(User user : users){
   %>
   <tr>
   <td>${users[0].id}</td>
   <td>${users[0].userName}</td>
   <td>${users[0].pwd}</td>  
   <td>${users[0].sex}</td>
   <td>${users[0].['age']}</td></tr>
   <%
  // }
    %>
   
  </tbody>
  </table>  
  </body>
</html>

第二种

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.zhangsan.bean.User"%>
<!-- jsp页面引入jstl标签库-->
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
	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>
		<title>My JSP 'userList.jsp' starting page</title>
	</head>
	<body>
		<table>
			<thead>
				<tr>
					<td>
						编号
					</td>
					<td>
						用户名
					</td>
					<td>
						密码
					</td>
					<td>
						性别
					</td>
					<td>
						年龄
					</td>
				</tr>
			</thead>
			<c:forEach items="${users}" var="user">
				<tbody>
					<tr>
						<th>
							${user.id}
						</th>
						<th>
							${user.userName }
						</th>
						<th>
							${user.pwd }
						</th>
						<th>
							${user.sex }
						</th>
						<th>
							${user.age }
						</th>
					</tr>
			</c:forEach>
			</tbody>
		</table>
	</body>
</html>

LoginServlet服务代码

package org.zhangsan.test;

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;

public class LoginServlet extends HttpServlet {

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

		doPost(request, response);
	}

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

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

		String userName = request.getParameter("userName");
		String pwd = request.getParameter("pwd");
		
		boolean Islogin= SelectTest.login(userName, pwd);
		
		if (Islogin=true) {
			response.sendRedirect("success.jsp");
		}else {
			response.sendRedirect("login.jsp");
		}

	}

}

RegSertvlet服务代码

package org.zhangsan.test;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

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

import org.zhangsan.bean.User;
import org.zhangsan.util.DBUtil;

public class RegServlet extends HttpServlet {

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

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

		String userName = request.getParameter("userName");
		String pwd = request.getParameter("pwd");
		String ageStr = request.getParameter("age");
		Integer age = Integer.parseInt(ageStr);
		String sex = request.getParameter("sex");

		String sql = "insert into users values(?,?,?,?)";

		Object[] objs = { userName, pwd, age, sex };

		int count = DBUtil.update(sql, objs);

		if (count > 0) {		
			//response.sendRedirect("login.jsp");
			ArrayList<User> users=UserManager.getUsers();	
			
			request.setAttribute("users", users);
			request.getRequestDispatcher("userList02.jsp").forward(request, response);
		} else {
			response.sendRedirect("reg.jsp");
		}
	}
}

bean层代码

package org.zhangsan.test;

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;

public class LoginServlet extends HttpServlet {

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

		doPost(request, response);
	}

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

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

		String userName = request.getParameter("userName");
		String pwd = request.getParameter("pwd");
		
		boolean Islogin= SelectTest.login(userName, pwd);
		
		if (Islogin=true) {
			response.sendRedirect("success.jsp");
		}else {
			response.sendRedirect("login.jsp");
		}

	}

}

DBUtil工具类

package org.zhangsan.test;

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;

public class LoginServlet extends HttpServlet {

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

		doPost(request, response);
	}

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

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

		String userName = request.getParameter("userName");
		String pwd = request.getParameter("pwd");
		
		boolean Islogin= SelectTest.login(userName, pwd);
		
		if (Islogin=true) {
			response.sendRedirect("success.jsp");
		}else {
			response.sendRedirect("login.jsp");
		}

	}

}

UserManager类

package org.zhangsan.test;

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;

public class LoginServlet extends HttpServlet {

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

		doPost(request, response);
	}

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

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

		String userName = request.getParameter("userName");
		String pwd = request.getParameter("pwd");
		
		boolean Islogin= SelectTest.login(userName, pwd);
		
		if (Islogin=true) {
			response.sendRedirect("success.jsp");
		}else {
			response.sendRedirect("login.jsp");
		}

	}

}

select查询类代码

package org.zhangsan.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.zhangsan.util.DBUtil;

public class SelectTest {

	public static boolean login(String userName, String pwd) {

		Connection conn = DBUtil.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from users where user_name=? and pwd=?";
		Boolean Islogin = false;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {

				Islogin = true;
			} else {
				Islogin = false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		DBUtil.close(conn, ps, null);
		return Islogin;
	}

}

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>LoginServlet</servlet-name>
    <servlet-class>org.zhangsan.test.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>RegServlet</servlet-name>
    <servlet-class>org.zhangsan.test.RegServlet</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/loginServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>RegServlet</servlet-name>
    <url-pattern>/regServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

10.13

©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页