第一种是jdbc+mysql+jsp简单登录页面
第二种是jdbc+mysql+jsp+servlet+xml简单登录页面
首先这是项目链接:https://pan.baidu.com/s/121KP74EKieJIgavbhz7Oow 密码:qmeu
因为这个项目很简单,只要有一定数据库基础和jsp基础,基本都可以做出来,所以也不会解释代码流程等等。
第一种,项目结构 第二种,项目结构
数据库
第一种代码jsp页面
validate.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
验证页面:
<%--获取用户名输入的用户名和密码:使用JavaBean类User类封装 --%>
<%
out.println(user.getUsername());
out.println("
");
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");
}
%>
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+"/";
%>
Insert title here
main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
欢迎你!
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"%>
Insert title here
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
student
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
LoginServlet
com.lisonglin.servlet.LoginServlet
LoginServlet
/login
以上就是简单的登录jsp代码,下一个博客把利用了连接池c3p0,dbutdbutils来写增删改查和分页。
欢迎大家支持我!有稳定收入的各位可以支持一下,谢谢,支付宝二维码!