JavaWeb两种方式登录

第一种是jdbc+mysql+jsp简单登录页面

第二种是jdbc+mysql+jsp+servlet+xml简单登录页面

首先这是项目链接:https://pan.baidu.com/s/121KP74EKieJIgavbhz7Oow 密码:qmeu

因为这个项目很简单,只要有一定数据库基础和jsp基础,基本都可以做出来,所以也不会解释代码流程等等。

第一种,项目结构                                              第二种,项目结构

jsp+mysql+jdbc项目演示                          jdbc+mysql+jsp+servlet

数据库

第一种代码jsp页面

validate.jsp

<%@ page language="java" import="java.util.*"  contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
	验证页面:<br>
	<%--获取用户名输入的用户名和密码:使用JavaBean类User类封装 --%>
	<jsp:useBean id="user" class="entity.User" scope="request"></jsp:useBean>
	<jsp:setProperty property="*" name="user"/>
	<jsp:useBean id="userSerivce" class="service.UserService"></jsp:useBean>
	<%
		out.println(user.getUsername());
	    out.println("<br/>");
	    out.print(user.getPwd()); 
	    if(userSerivce.valiUser(user)){
	    	out.print("登陆成功!");
	    	//将用户信息保存到session中
	    	session.setAttribute("user",user);
	    	response.sendRedirect("main.jsp");
	    }else{
	    	out.print("登录失败!");
	    	response.sendRedirect("index.jsp");
	    }
	%>
</div>
</body>
</html>

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<base href="<%=basePath%>"/>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="validate.jsp">
	<h3>用户登录</h3>
	账户:<input type="text" name="username"/><br>
	密码:<input type="password" name="pwd" /><br> 
	<input type="submit" name="button" value="登录"/>
</form>
</body>
</html>

main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
欢迎你!
</body>
</html>

java代码

DataUtil.java

package util;

import java.sql.Connection;
import java.sql.DriverManager;
public class DataUtil {
	
	public static Connection getConnection() {
		Connection con = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = 	DriverManager.getConnection("jdbc:mysql://localhost:3306/user?usecode=true&characterEncoding=utf-8","root","123");//三个参数:数据库地址,用户名,密码
			System.out.print(con.getMetaData().getURL());
			return con;	
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
}

业务层,没写dao,算了,懒得改了,你们自己按照第二种结构写

package service;

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

import entity.User;
import util.DataUtil;

public class UserService {
	private Connection con = null;
	private PreparedStatement pst = null;
	
	public UserService() {
		con = DataUtil.getConnection();
	}
	
	//编写登录验证方法valiUser()
	public boolean valiUser(User user){
		try{
			pst = con.prepareStatement("select * from user where username=? and pwd=?");
			pst.setString(1, user.getUsername());
			pst.setString(2, user.getPwd());
			ResultSet rs =pst.executeQuery();
			if(rs.next()){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
}

user.java

package entity;

public class User {
	private String username;
	private String pwd;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

}

第二种代码

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post" action="login">
	<h3>用户登录</h3>
	账户:<input type="text" name="username"/><br>
	密码:<input type="password" name="pwd" /><br> 
	<input type="submit" name="button" value="登录"/>
</form>
</body>
</html>

conn.java

package com.lisonglin.util;

import java.sql.Connection;
import java.sql.DriverManager;

public class Conn {
	public static Connection getConnection() {
		Connection conn=null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?usecode=true&characterEncoding=utf-8", "root","123");
//			System.out.print(conn.getMetaData().getURL());
			return conn;	
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
}

student.java

package com.lisonglin.entity;

public class Student {
	private String userName;
	private String pwd;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	
}

studentDao.java

package com.lisonglin.dao;

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

import com.lisonglin.entity.Student;
import com.lisonglin.util.Conn;

public class StudentDao {
	private Connection con = null;
	private PreparedStatement pst = null;
	
	public StudentDao() {
		con =Conn.getConnection();
	}
	
	//编写登录验证方法valiUser()
	public boolean valiUser(Student user){
		try{
			pst = con.prepareStatement("select * from user where username=? and pwd=?");
			pst.setString(1, user.getUserName());
			pst.setString(2, user.getPwd());
			ResultSet rs =pst.executeQuery();
			if(rs.next()){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
}

studentService

package com.lisonglin.service;

import com.lisonglin.dao.StudentDao;
import com.lisonglin.entity.Student;

public class StudentService {
	
	public boolean user(Student user) {
	    return new StudentDao().valiUser(user);
	}
	
}

LoginServlet.java

package com.lisonglin.servlet;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.lisonglin.dao.StudentDao;
import com.lisonglin.entity.Student;
import com.lisonglin.service.StudentService;

public class LoginServlet implements Servlet {
	
	@Override
	public void destroy() {

	}

	@Override
	public ServletConfig getServletConfig() {
		return null;
	}

	@Override
	public String getServletInfo() {
		return null;
	}

	@Override
	public void init(ServletConfig config) throws ServletException {

	}

	@Override
	public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		System.out.println("send servlet service");
		//提交的数据有可能有中文, 怎么处理。
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		//1. 获取客户端提交的信息
		String userName = request.getParameter("username");
		String password = request.getParameter("pwd");
		StudentService ss=new StudentService();
		System.out.println("username:"+userName+",pwd:"+password);
		Student user=new Student();
		user.setUserName(userName);
		user.setPwd(password);
		if(ss.user(user)) {
			response.getWriter().write("欢迎你!"+user.getUserName());
		}else {
			response.getWriter().write("用户名或者密码错误!");
		}
	}

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>student</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>com.lisonglin.servlet.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  		<servlet-name>LoginServlet</servlet-name>
  		<url-pattern>/login</url-pattern>
  </servlet-mapping>
</web-app>

以上就是简单的登录jsp代码,下一个博客把利用了连接池c3p0,dbutdbutils来写增删改查和分页。

猜你喜欢

转载自blog.csdn.net/qq_41520636/article/details/81606000
今日推荐