Servlet基础 管理员注册页面

管理员注册页面

index.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>注册页面</title>

</head>

<body>
<form action="AdminServlet">
管理员姓名:<input name="adminname"><br>
管理员密码:<input name="adminpwd"><br>
<input type = "submit" value="注册"><input type="reset" value="重置">
</form>

</body>
</html>

AdminServlet.java

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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

import com.bean.Admin;
import com.dao.IAdminDao;
import com.dao.impl.AdminDaoImpl;

public class AdminServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置请求的编码格式
response.setCharacterEncoding("utf-8");

IAdminDao adminDao = new AdminDaoImpl();
//获取表单提交的数据
String name = request.getParameter("adminname");
System.out.println(name);
String pwd = request.getParameter("adminpwd");
System.out.println(pwd);
//name 赋值到admin.adminName
//pwd 赋值到admin.adminPWd
Admin admin = new Admin();
admin.setAdminName(name);
admin.setAdminpwd(pwd);
//接收数据保存到数据库
//调用AdminDaoImpl的实现
int i = adminDao.regAdmin(admin);
//判断注册是否成功
if(i>0){
System.out.println("注册成功!");
//响应头
/*response.setHeader("user", "caixukun");*/
//方法1、设置响应的编码格式
/*response.setCharacterEncoding("utf-8");
response.setHeader("Content-type","text/html;charset=utf-8");*/
//方法2、设置响应的编码格式
response.setContentType("text/html;charset=utf-8");

//字节流输出
/*ServletOutputStream steam = response.getOutputStream();
String mag = "zijieliu";
steam.write(mag.getBytes());*/

//字符流输出
PrintWriter writer = response.getWriter();
writer.write("<h1>注册成功!</h1>");
writer.write("<h1>点击<a href = 'login.jsp'>这里</a>进入管理员登陆页面</h1>");
}else{
System.out.println("注册失败!");
response.getWriter().write("<h1>注册失败!请重新注册</h1>");
response.getWriter().write("<h1><a href = 'index.jsp'>返回管理员注册页面</a></h1>");
}
}

}

IAdminDao.java

package com.dao;

import java.sql.ResultSet;

import com.bean.Admin;

public interface IAdminDao {
//注册的接口
int regAdmin(Admin admin);

//查询登录的接口
ResultSet login(Admin admin);

}
AdminDaoImpl.java

package com.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.bean.Admin;
import com.dao.IAdminDao;
import com.mysql.jdbc.Connection;
import com.utils.BaseDao;

public class AdminDaoImpl implements IAdminDao {

//注册功能实现
@Override
public int regAdmin(Admin admin) {
int i = 0;
Connection conn = BaseDao.getconn();
String sql = "insert into admin values(null,?,?)";
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, admin.getAdminName());
ps.setObject(2, admin.getAdminpwd());
i = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}

//登陆功能的判断
@Override
public ResultSet login(Admin admin) {
ResultSet rs = null;
//1、
Connection conn = BaseDao.getconn();
String sql = "select * from admin where admin_name = ? and admin_pwd = ?";
try {
PreparedStatement ps = conn.clientPrepareStatement(sql);
ps.setObject(1, admin.getAdminName());
ps.setObject(2, admin.getAdminpwd());
rs = ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return rs;
}

}

BaseDao.java

package com.utils;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;

import com.sun.jndi.url.corbaname.corbanameURLContextFactory;

public class BaseDao {

private static final String driver = "com.mysql.jdbc.Driver";

private static final String url = "jdbc:mysql:///news_week1?characterEncoding=utf-8";

private static final String user = "root";

private static final String pwd = "root";

static{

try {

Class.forName(driver);

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static Connection getconn(){

Connection conn = null;

try {

conn = DriverManager.getConnection(url, user, pwd);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return conn;

}

public static void close(Connection conn,PreparedStatement ps,ResultSet rs){

try {

if(null !=rs)

rs.close();

if(null !=ps)

ps.close();

if(null !=conn)

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

Admin.java

package com.bean;

public class Admin {

private int adminId;
private String adminName;
private String adminpwd;
public int getAdminId() {
return adminId;
}
public void setAdminId(int adminId) {
this.adminId = adminId;
}
public String getAdminName() {
return adminName;
}
public void setAdminName(String adminName) {
this.adminName = adminName;
}
public String getAdminpwd() {
return adminpwd;
}
public void setAdminpwd(String adminpwd) {
this.adminpwd = adminpwd;
}
@Override
public String toString() {
return "Admin [adminId=" + adminId + ", adminName=" + adminName

  • ", adminpwd=" + adminpwd + "]";
    }public Admin(int adminId, String adminName, String adminpwd) {
    super();
    this.adminId = adminId;
    this.adminName = adminName;
    this.adminpwd = adminpwd;
    }public Admin() {
    super();
    }
    }

管理员登陆页面

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>登陆页面</title>

</head>
<body>
<h1>管理员登陆页面</h1>
<form action = "LoginServlet" method="post">
管理员姓名:<input type = "text" name = "adminname"><br>
管理员密码:<input type = "text" name = "adminpwd"><br>
<input type = "submit" value = "登陆"><input type = "submit" value = "取消"><br>
</form>

</body>

</html>

LoginServlet.java

package com.servlet;

import java.io.IOException;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.bean.Admin;

import com.dao.IAdminDao;

import com.dao.impl.AdminDaoImpl;

public class LoginServlet extends HttpServlet {

@Override

protected void service(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

IAdminDao dao = new AdminDaoImpl();

//处理乱码

request.setCharacterEncoding("utf-8");

//设置响应的编码格式

response.setCharacterEncoding("utf-8");

response.setContentType("text/html;charset=utf-8");

//接收表单数据

String name = request.getParameter("adminname");

String pwd = request.getParameter("adminpwd");

//连接数据库判断是否存在登录的用户

Admin admin = new Admin();

admin.setAdminName(name);

admin.setAdminpwd(pwd);

//调用接口实现查询登陆用户功能

ResultSet rs = dao.login(admin);

//判断登陆是否成功

try {

if(rs.next()){

//response.getWriter().write("<h1>恭喜您登陆成功!</h1>");

拓展 转发和重定向

/*转发的形式实现页面跳转 request.getRequestDispatcher
http://localhost:8080/adminDemo1/LoginServlet
request.setAttribute("user", name);
request.getRequestDispatcher("success.jsp").forward(request, response);*/
/*重定向的方式实现页面跳转 response.sendRedirect
http://localhost:8080/adminDemo1/success.jsp
response.sendRedirect("success.jsp");*/

/*
* 转发和重定向的区别
* 1、地址栏不同,转发地址栏不发生改变,重定向地址栏改变为重定向的页面。
* 2、request 作用域一次请求的参数,转发可以共享,可以取request作用域的值,转发不可以。
* 3、转发是一次请求,重定向是两次请求。
* 4、转发只能跳转到本项目下的路径,重定向只要网络允许可以跳转任意路径。
*/

}else {

response.getWriter().write("<h1>登陆失败!</h1>");

response.getWriter().write("<a href = 'login.jsp'>返回登陆界面!</a>");

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

拓展

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 'success.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>
<h1>欢迎${user},登陆成功</h1>
</body>
</html>

相关推荐
桀桀桀桀桀桀28 分钟前
数据库中的用户管理和权限管理
数据库·mysql
代码之光_198037 分钟前
保障性住房管理:SpringBoot技术优势分析
java·spring boot·后端
ajsbxi42 分钟前
苍穹外卖学习记录
java·笔记·后端·学习·nginx·spring·servlet
StayInLove1 小时前
G1垃圾回收器日志详解
java·开发语言
对许1 小时前
SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“
java·log4j
无尽的大道1 小时前
Java字符串深度解析:String的实现、常量池与性能优化
java·开发语言·性能优化
小鑫记得努力1 小时前
Java类和对象(下篇)
java
binishuaio1 小时前
Java 第11天 (git版本控制器基础用法)
java·开发语言·git
zz.YE1 小时前
【Java SE】StringBuffer
java·开发语言
老友@1 小时前
aspose如何获取PPT放映页“切换”的“持续时间”值
java·powerpoint·aspose