由于对于前端的代码不熟悉,页面登陆界面会比较简陋,请见谅。
本博客为本人自学后自行尝试的实战,若有错误,望指出,不胜感激。
在eclipse中,整个项目结构如下:
login.jsp用于登陆,register.jsp用于注册,welcome.jsp为登陆后的界面
linkdb.java主要用来连接数据库,代码如下:
package secondsfj;
import java.sql.DriverManager;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
public class linkdb {
public static final String db="com.mysql.jdbc.Driver";
public static final String url="jdbc:mysql://localhost:3306";
public static final String user="root";
public static final String password="9999999";
private Connection conn;
public void linkdatabase()
{
try {
Class.forName(db);
conn=(Connection) DriverManager.getConnection(url,user,password);
} catch (ClassNotFoundException e) {
System.out.println("加载数据库类失败");
} catch (SQLException e) {
System.out.println("连接数据库失败");
}
}
public Connection getconn()
{
return conn;
}
public void close()
{
try {
if(!conn.isClosed())conn.close();
} catch (SQLException e) {
System.out.println("关闭数据库连接失败");
}
}
}
controldb.java主要用于数据库的操作,代码如下:
package secondsfj;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;
import secondsfj.linkdb;
public class controldb {
private Statement state=null;
private ResultSet res = null;
linkdb deal=new linkdb();
public void dealcontroldb()//连接数据库,创建SQL语句对象
{
try {
deal.linkdatabase();
state=(Statement) (deal.getconn()).createStatement();
} catch (SQLException e) {
System.out.println("创建SQL语句失败");
}
hasdb();
}
void hasdb()//判断数据库是否存在,不存在,进行创建
{
String temp;
boolean x=false;
try {
res=state.executeQuery("show databases;");
while(res.next())
{
temp=res.getString(1);
if(temp.equals("login"))
{
x=true;
break;
}
}
if(x==false)
{
state.execute("create database login;");
state.execute("use login;");
state.execute("create table user (username char(20),password char(20));");
}
} catch (SQLException e) {
System.out.println("controldb中的SQL语句执行失败");
}
}
public boolean verify(String username,String password)//用于验证登陆
{
String usertemp;
String passtemp;
if(username==null||password=="null"||"".equals(username)||"".equals(password)) return false;
try {
state.execute("use login;");
res=state.executeQuery("select * from user");
while(res.next())
{
usertemp=res.getString(1);
if(usertemp.equals(username))
{
passtemp=res.getString(2);
if(passtemp.equals(password)) return true;
}
}
} catch (SQLException e) {
System.out.println("verify中的SQL语句执行失败");
}
return false;
}
public boolean register(String user,String passwd)//用于注册
{
if(user==null||passwd==null||"".equals(user)||"".equals(passwd)) return false;
try {
state.execute("use login;");
state.execute("insert into user(username,password) values("+user+","+passwd+");");
} catch (SQLException e) {
System.out.println("register中的SQL语句执行失败");
return false;
}
return true;
}
public void close()//关闭连接
{
try {
if(!state.isClosed())state.close();
if(!res.isClosed())res.close();
deal.close();
} catch (SQLException e) {
System.out.println("controldb关闭失败");
}
}
}
servletforlogin用于处理登陆界面的post请求,代码如下:
import java.io.IOException;
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 secondsfj.controldb;
import secondsfj.linkdb;
@WebServlet(displayName="servletforlogin", urlPatterns={"/servletforlogin"})
public class servletforlogin extends HttpServlet {
private static final long serialVersionUID = 1L;
public servletforlogin() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("user");
String password=request.getParameter("password");
controldb temp=new controldb();
temp.dealcontroldb();
boolean x=temp.verify(username, password);
String error=null;
if(x==false)
{
error="用户名或是密码错误";
request.setAttribute("error", error);
request.getRequestDispatcher("login.jsp").forward(request, response);
}
else
{
response.sendRedirect("welcome.jsp");
}
temp.close();
}
}
servletforregister负责注册,代码如下:
import java.io.IOException;
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 secondsfj.controldb;
import secondsfj.linkdb;
@WebServlet(displayName="servletforregister",urlPatterns={"/servletforregister"})
public class servletforregister extends HttpServlet {
private static final long serialVersionUID = 1L;
public servletforregister() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("user");
String password=request.getParameter("password");
controldb deal=new controldb();
deal.dealcontroldb();
String error=null;
boolean x=deal.register(username, password);
if(x==false)
{
error="密码或是用户名为空";
request.setAttribute("error", error);
request.getRequestDispatcher("register.jsp").forward(request, response);
}
else response.sendRedirect("login.jsp");
deal.close();
}
}
login.jsp为登陆界面,代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
登陆
<%
String deal=(String)request.getAttribute("error");
if(deal!=null)
out.println(deal);
%>
register.jsp用于注册,代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
注册
<%
String error=(String)request.getAttribute("error");
if(error!=null)
out.println(error);
%>
welcome.jsp为登陆后的界面,代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
欢迎!!!!!
运行过程如下,先注册后登陆:
若我们登陆时输入错误的用户或密码,会出现错误提示:
注意到url处不为login.jsp,因为我们调用的是forward,此时地址栏的url不会发生改变。
若我们注册时只输入空格或不输入就按提交,会出现如下错误信息
url栏不变原因同上。