JavaWeb中登录功能

JavaWeb中登录功能

JavaWeb登陆的基本流程:JSP页面发送请求——>Servlet——>Servlet通过调用方法从数据库中得到数据并将结果返回页面。
先建立三个jsp页面,包括login.jsp(登陆页面)、index.jsp(显示登陆成功后的信息)、error.jsp(登录失败的页面)。

login.jsp
需要将pageEncoding="ISO-8859-1"改为pageEncoding=“UTF-8”


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<form action="LoginServlet" method="post">
		用户名:<input type="text" name="userName"/><br>
		密码:<input type="password" name="password"/><br>
		<input type="submit" value="submit"/>
	</form>

</body>
</html>

index.jsp


<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
this is index.jsp<br>
(这个界面需要显示登陆成功后的信息 )
</body>
</html>

error.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
this is error.jsp
</body>
</html>

建立User类,生成get和set方法


package day0302Login;

public class User {
	private String userName;
	private String password;
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
}

jsp页面中的action=“LoginServlet”是指将请求发送到Servlet处理


package day0302Login;

import java.io.IOException;

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

//创建时为Servlet而不是Class,需要在web.xml中进行配置,配置的代码Myeclipse将自动生成
public class LoginServlet extends HttpServlet {
	
	//创建UserDao的对象,以便于查询数据库
//	UserDao userDao=new UserDao();
	
	//以下doGet方法和doPost方法分别对应form表单中的method="get"和method="post"
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
//		super.doGet(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
//		super.doPost(request, response);
		
		//利用getParameter方法获取到前台文本框中输入的值,其中括号内的内容为<input/>标签中的name属性
		String userName=request.getParameter("userName");
		String password=request.getParameter("password");
		/*
		
		//调用UserDao中的getSelect方法并获取到返回值。	//调用
		boolean flag=userDao.getSelect(userName, password);
		//若用户名和密码存在则转发到index.jsp页面,否则重定向到error.jsp页面
		if (flag) {
			//转发
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
		else
			//重定向
			response.sendRedirect("error.jsp");
			
		*/
	}
	
}

首先连接数据库,然后在查询的方法中传入从jsp页面获取到的userName和password,判断数据库中是否存在此用户名和密码的用户,如果存在则返回true,否则返回false(不要忘记导入数据库链接的包)。
至于数据库中的字段则参照实体类User建立,即包含userName和password两个属性。


package day0302Login;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDao {
	// 连接数据库的代码
	public Connection getCon() {
		// 数据库连接名称
		String username = "root";
		// 数据库连接密码
		String password = "";
		String driver = "com.mysql.jdbc.Driver";
		// 其中test为数据库名称
		String url = "jdbc:mysql://localhost:3306/test";
		Connection conn = null;
		try {
			Class.forName(driver);
			conn = (Connection) DriverManager.getConnection(url, username,
					password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	// 进行查询的方法,若含有满足条件的数据则返回true
	public boolean getSelect(String userName, String password) {
		boolean flag = false;
		String sql = "select * from user where userName='" + userName
				+ "' and password='" + password + "'";
		Connection conn = getCon();
		PreparedStatement pst = null;
		try {
			pst = (PreparedStatement) conn.prepareStatement(sql);
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				flag = true;
			}
		} catch (Exception e) {
		}
		return flag;
	}
}

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>day0302Login</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>day0302Login.LoginServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>
  
</web-app>

在这里插入图片描述


  • 5
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值