Web实验-Servlet控制层设计HTML-分层

云惠网小编 2021年12月3日01:20:17
评论
23426字阅读78分5秒
摘要

Web实验-Servlet控制层设计实验内容:使用Servlet和四层结构知识,改进用户信息管理系统,使其具有更好的软件结构,以及提升可读性和可扩展性。实验步骤:1.创建Web项目——WebServlet2.源代码模块实验步骤:数据库连接层:DBConn.javapackage util;import java.sql.*;public class DBConn { public Connection getConnection(){ Connection conn=nul

广告也精彩

实验内容:
使用Servlet和四层结构知识,改进用户信息管理系统,使其具有更好的软件结构,以及提升可读性和可扩展性。
在这里插入图片描述

package util;
import java.sql.*;
public class DBConn {
public Connection getConnection(){
Connection conn=null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}catch(ClassNotFoundException e)
{
e.printStackTrace();
System.out.println("加载异常");
return null;
}
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=School";//ST为需要连接的数据库名
String user = "sa";//以sql账户登录,账号密码根据自己的数据库设计
String password = "sa";//密码
try{
conn=DriverManager.getConnection(url,user,password);
}catch(SQLException e)
{
e.printStackTrace();
System.out.println("连接异常");
return null;
}
System.out.println("连接成功");
return conn;
}
//关闭数据库
public void closeConn(Connection conn){
if(conn !=null){
try {
conn.close();
System.out.println("关闭数据库成功");
}catch(SQLException e)
{
e.printStackTrace();
}	}		}
//public static void main(String[] args){
//DBConn obj=new DBConn();
//	obj.	
//}
}
package dao;
import java.sql.*;
import java.util.*;
import entity.User;
import util.DBConn;
public class UserDao {
private Connection conn = null;
private PreparedStatement ptst= null;
private ResultSet rs= null;
String m1=null,m2=null,m3=null;
int n1=0,n2=0,n3=0;
//查询	
public ArrayList<User> getthe2(){
DBConn obj=new DBConn();
conn=obj.getConnection();
ArrayList<User> users=new ArrayList<User>();
if(conn==null){
System.out.println("连接异常");
return null;
}
String sql="select * from Login";
try{			conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);			ptst=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=ptst.executeQuery();
rs.last();
rs.beforeFirst();
while(rs.next()){
User user=new User();
user.setUser_id(rs.getString("user_id"));
user.setUser_name(rs.getString("user_name"));
user.setUser_password(rs.getString("user_password"));
user.setUser_age(rs.getString("user_age"));
users.add(user);
System.out.println("数据库正常");
}
}catch(SQLException e){
e.printStackTrace();
System.out.println("数据库异常");
return null;
}
return users;
}
//修改	
public void changeservlet(String user_id,String user_name,String user_password,String user_age){
try {
Connection conn = null;
DBConn obj=new DBConn();
conn=obj.getConnection();
PreparedStatement stmt = null;
System.out.println("Connection Successful!");
String sql="update Login set user_name=?,user_password=?,user_age=? where user_id =?";
stmt=conn.prepareStatement(sql);
stmt.setString(1, user_name);
stmt.setString(2,user_password);
stmt.setString(3, user_age);
stmt.setString(4, user_id);
stmt.executeUpdate();
System.out.println(user_name);
System.out.println("修改成功");
}catch(SQLException e){
e.printStackTrace();
System.out.println(user_id+"失败");
System.out.println("数据修改异常");
}
}
//删除
public void deleteservlet(String user_id){
try{
Connection conn = null;
PreparedStatement pstmt =null;
DBConn obj=new DBConn();
System.out.println("2");
conn=obj.getConnection();
System.out.println("3");
System.out.println("shachu Connection Successful!");
String sql = "use School DELETE FROM  Login WHERE user_id=?";
System.out.println(user_id+"成功");
pstmt=conn.prepareStatement(sql);
pstmt.setString(1, user_id);
System.out.println("4");
pstmt.executeUpdate();
System.out.println("数据删除成功!");
}catch(SQLException e){
e.printStackTrace();
System.out.println(user_id+"失败");
System.out.println("数据删除异常");
}
}
//登录
public String loginservlet(String user_name){
//ArrayList<User> users=new ArrayList<User>();
User user=new User();
String password=null;
Connection conn = null;
PreparedStatement ptst= null;
ResultSet rs= null;
DBConn obj=new DBConn();
conn=obj.getConnection();
if(conn==null){
System.out.println("连接异常");
}
System.out.println("1");
try{
String sql = "use School SELECT * FROM  Login WHERE user_name=?";
conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
System.out.println("2");
ptst=conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);//初始化Sql
ptst.setString(1,user_name);
rs=ptst.executeQuery();//执行SQL语句		
rs.last();
rs.beforeFirst();
while(rs.next()){
user.setUser_password(rs.getString("user_password"));
//  users.add(user);
System.out.println("数据库正常"+user.getUser_password());
password=user.getUser_password();
System.out.println("数据库正常"+user.getUser_password().length());
}
if(password==null||password==""){
password="1111";
}
}catch(SQLException e){
e.printStackTrace();
System.out.println("数据库异常");
}
return password;
}
public void closeAll(){
try {
if (rs != null) {
rs.close();
}
if (ptst != null) {
ptst.close();
}
if (conn != null) {
conn.close();
}
}catch(Exception e) {
}
}
}
<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8" 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 'Funtion.jsp' starting page</title>
<style>
body{text-align:center}
</style>
</head>
<style type="text/css">
.mytable{
width:780px;
height:380px;
margin:0 auto;
text-align:center;
font-size:35px;
}
.mytable1{
font-weight:1000;
width:100%;
height:100%;
margin:0 auto;
text-align:center;
background-color:#ffffff;
}
.shuju{
height:20px;
font-size:20px;
}
</style>
<body>
<table class="mytable" border="3">
<tr>
<td style="background-color:#ACD6FF;font-size:5px;height:10px;text-align:center;color:aliceblue">
<h1>功能</h1>
</td>
</tr>
<tr>
<td>
<a href="Myadd.jsp" target="_parent">前往注册界面</a>
</td>
</tr>
<tr>
<td>
<a href="login.html" target="_parent">返回登录界面</a>
</td>
</tr>
<tr>
<td>
<a href="Login_user.jsp" target="_parent">管理注册账号</a>
</td>
</tr>
</table>
</body>
</html>

修改:change.jsp

查看注册用户界面:Login-User.jsp

Web实验-Servlet控制层设计HTML-分层
Web实验-Servlet控制层设计HTML-分层
删除界面:点击右侧按钮即可删除:
在这里插入图片描述

错误:error.jsp

Web表示层设计:
登录页面:login.html

package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.UserDao;
public class login extends HttpServlet {
private static final long serialVersionUID = 618236416112505770L;
int i=0;
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
String password="error";
//从request接收表单数据
request.setCharacterEncoding("GBK");
String name=request.getParameter("user_name");
if(name==null||name==""){
name="1111";
}
String pass=request.getParameter("user_password");
System.out.println(name+pass);
UserDao userdao=new UserDao();
System.out.println("1");
password=userdao.loginservlet(name);
System.out.println("----" + password.length());
//通过response向客户端应答,显示接收到的数据
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>处理表单</TITLE></HEAD>");
out.println("<BODY>");
password=password.trim();
System.out.println("----" + password.length());
System.out.println("----" + pass.length());
System.out.println(pass.equals(password));
if(pass.equals(password))
{
System.out.println("3");
try{
System.out.println("跳转正常");
//response.sendRedirect("../../WebRoot/Login_user.jsp");//请求转发跳转
request.getRequestDispatcher("Funtion.jsp").forward(request, response);//请求转发跳转
}catch (Exception e){
e.printStackTrace();
System.out.println("跳转异常");
}
}else{
System.out.println("4");
out.println("密码错误");
out.println("<A HREF='https://blog.csdn.net/Tian208/article/details/121648368/login.html' TARGET='_parent'>返回</A>");
out.println("<BODY>");
}
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.UserDao;
public class change extends HttpServlet{
private static final long serialVersionUID = 618236416112505770L;
int i=0;
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
//获得初始化参数
String ouserName=this.getInitParameter("ouserName");
String ouserPwd=this.getInitParameter("ouserPwd");
System.out.println(ouserName+ouserPwd);
//从request接收表单数据
request.setCharacterEncoding("UTF-8");
String user_id=request.getParameter("user_id");
String user_name=request.getParameter("user_name");
String user_password=request.getParameter("user_password");
String user_age=request.getParameter("user_age");
System.out.println(user_name+user_password);
i=1;
UserDao userdao=new UserDao();
userdao.changeservlet(user_id,user_name,user_password,user_age);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>处理表单</TITLE></HEAD>");
out.println("<BODY>");
System.out.println("修改中");
if(i==1){
System.out.println("修改成功");
request.getRequestDispatcher("Login_user.jsp").forward(request, response);//请求转发跳转
}else{
System.out.println("修改失败");
}
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}

数据库操作层:UserDao.java

登录成功跳转到功能界面:
在这里插入图片描述

登录界面:
在这里插入图片描述

功能界面:Funtion.jsp

package entity;
public class User {
private String sno; // 学号	
private String sname; // 姓名
private String user_name;
private String user_password;
private String user_age;
private String user_id;
/*public User(String sno,String sname,int sage) {
super();//表示调用父类的构造方法
this.sno = sno;
this.sname=sname;
this.sage=sage;
}*/
public String getSno()
{
return sno;
}
public void setSno(String sno)
{
this.sno = sno;
}
public String getSname()
{
return sname;
}
public void setSname(String sname)
{
this.sname = sname;
}
public String getSage()
{
return sage;
}
public void setSage(String sage)
{
this.sage = sage;
}
public String getUser_id() {
return user_id;
}
public void setUser_id(String user_id) {
this.user_id = user_id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getUser_password() {
return user_password;
}
public void setUser_password(String user_password) {
this.user_password = user_password;
}
public String getUser_age() {
return user_age;
}
public void setUser_age(String user_age) {
this.user_age = user_age;
}
public void clears(String user_id,String user_name,String user_password,String user_age){
this.user_id=user_id;
this.user_name=user_name;
this.user_password=user_password;
this.user_age=user_age;
}
}

删除:delete.java

修改 chang.java

<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "dao.UserDao" %>
<%@ page import = "entity.User" %>
<%@ page import = "util.DBConn" %>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id="add" class="entity.User"></jsp:useBean>
<jsp:useBean id="bdd" class="dao.UserDao"></jsp:useBean>
<jsp:useBean id="cdd" class="util.DBConn"></jsp:useBean>
<jsp:setProperty property="*" name="add"/>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>
显示数据库中的信息
</title>
<script type="text/javascript">
function del(user_ids){
if(confirm("确认删除本条记录吗?")){
// alert(user_ids);
window.location.href= "servlet/delete?user_ids="+user_ids;
}else{
}
}
function cha(user_ids){
if(confirm("确认修改本条记录吗?")){
//  alert(user_ids);
window.location.href= "change.jsp?user_ids="+user_ids;
}else{
}
}
function onClicka(){
window.location.href="Myadd.jsp";
}
function onClicka2(){
window.location.href="login.html";
}
</script>
</head>
<style type="text/css">
.mytable{
width:1480px;
height:100%;
margin:0 auto;
font-weight: 200;
text-align:center;
}
.mytable1{
font-weight:1000;
width:100%;
height:100%;
margin:0 auto;
text-align:center;
background-color:#ffffff;
}
.shuju{
height:20px;
font-size:20px;
}
</style>
<body>
<%
UserDao UserDao1=new UserDao();
ArrayList<User> Users=UserDao1.getthe2();
if(Users ==null){
Users =new ArrayList<User>();
}
%>
<form action="delete" method="post">
<table class="mytable" border="3";>
<tr>
<td colspan="9" style="background-color:#ACD6FF;font-size:10px;height:10px;text-align:center;color:aliceblue">
<h1>已注册用户</h1>
</td>
</tr>
<tr></tr>
<tr></tr>
<tr>
<th>-用户ID--</th>
<th>--用户名--</th>
<th>--密码--</th>
<th>--年龄--</th>
<th>--功能--</th>
<th>--功能--</th>
</tr>
<%
for (User user : Users){
String user_ids = user.getUser_id();
user_ids=new String(user_ids.getBytes("ISO-8859-1"),"utf-8");
%>
<tr>
<td><input disabled type="text" id="user_id" name="user_id"  value=<%=user.getUser_id()%>></td>
<td><input disabled type="text" id="user_name"  value=<%=user.getUser_name()%>></td>
<td><input  disabled type="text" id="user_password"  value=<%=user.getUser_password()%>></td>
<td><input  disabled type="text" id="user_age"  value=<%=user.getUser_age()%>></td>
<td> <input type="button" onClick="cha(<%=user_ids%>);"  value="修改"></td>
<td> <input type="submit" name="userids" value=<%=user.getUser_id()%>>删除</td>
</tr>
<%
}
%>
</table>
</form>
<table class="mytable">
<tr></tr>
<tr></tr>
<tr></tr>
<tr class="mytable1">
<td>------<input type="button" value="返回注册界面" style="background-color:#7B7B7B;width:150px;height:30px;
font-size:20px;"onClick="onClicka()"/>------
------<input type="button" value="返回登录界面" style="background-color:#7B7B7B;width:150px;height:30px;
font-size:20px;"onClick="onClicka2()"/>------
</td>
</tr>
</table>
</body>
</html>
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.UserDao;
public class delete extends HttpServlet {
private static final long serialVersionUID = 618236416112505770L;
int i=1;
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String user_id = request.getParameter("userids");
System.out.println(user_id);
System.out.println("1");
UserDao userdao=new UserDao();
userdao.deleteservlet(user_id);
//通过response向客户端应答,显示接收到的数据
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>处理表单</TITLE></HEAD>");
out.println("<BODY>");
if(i==1){
System.out.println("删除成功");
request.getRequestDispatcher("Login_user.jsp").forward(request, response);//请求转发跳转
}else{
System.out.println("删除失败");
}
out.println("</BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}

实体类User.java

实验步骤:
1.创建Web项目——WebServlet
Web实验-Servlet控制层设计HTML-分层
2.源代码模块
Web实验-Servlet控制层设计HTML-分层Web实验-Servlet控制层设计HTML-分层
实验步骤:
数据库连接层:DBConn.java

<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "dao.UserDao" %>
<%@ page import = "entity.User" %>
<%@ page import = "util.DBConn" %>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<jsp:useBean id="add" class="entity.User"></jsp:useBean>
<jsp:useBean id="bdd" class="dao.UserDao"></jsp:useBean>
<jsp:useBean id="cdd" class="util.DBConn"></jsp:useBean>
<jsp:setProperty property="*" name="add"/>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>
修改数据
</title>
</head>
<style type="text/css">
.mytable{
width:100%;
height:100%;
margin:0 auto;
font-weight: 200;
text-align:center;
}
.mytable1{
font-weight:1000;
width:100%;
height:100%;
margin:0 auto;
text-align:center;
background-color:#ffffff;
}
.shuju{
height:20px;
font-size:20px;
}
</style>
<body>
<form action="change" method="post">
<table class="mytable" border="8">
<%
request.setCharacterEncoding("UTF-8");
List<User> list1 = new ArrayList<User>();
String user_id1 = request.getParameter("user_ids");
String user_id=user_id1;
if(user_id1==null){
user_id=request.getParameter("user_id");
}
System.out.println(user_id);
String sql1 = "use School SELECT * FROM  Login WHERE user_id=?";
/*String user_name=request.getParameter("user_names");
user_name=new String(user_name.getBytes("ISO-8859-1"),"utf-8");
String user_password=request.getParameter("user_passwords");
String user_age=request.getParameter("user_ages");*/
System.out.println("1");
User user=new User();
try{
Connection conn = null;
ResultSet res1 = null;
PreparedStatement stmt1 = null;
UserDao user1=new UserDao();
DBConn obj=new DBConn();
System.out.println("2");
conn=obj.getConnection();
System.out.println("3");
System.out.println("queren Connection Successful!");
System.out.println(user_id+"成功");
stmt1=conn.prepareStatement(sql1);
stmt1.setString(1,user_id);
res1 = stmt1.executeQuery();
System.out.println("4");
while (res1.next())
{
user.setUser_id(res1.getString("user_id"));
user.setUser_name(res1.getString("user_name"));
user.setUser_password(res1.getString("user_password"));
user.setUser_age(res1.getString("user_age"));
list1.add(user);
}
System.out.println("5");
}catch(SQLException e){
e.printStackTrace();
System.out.println(user_id+"失败");
System.out.println("6");
}
user_id=user.getUser_id();
String user_name=user.getUser_name();
String user_password=user.getUser_password();
String user_age=user.getUser_age();
System.out.println("7"+user_name+user_password);
%>
<tr>
<td colspan="8" style="background-color:#ACD6FF;font-size:10px;height:10px;text-align:center;color:aliceblue">
<h1>修改数据</h1>
</td>
</tr>
<tr>
<tr></tr>
<tr></tr>
<tr class="mytable1">
<th>-用户ID--</th>
<th>--用户名--</th>
<th>--用户密码--</th>
<th>--年龄--</th>
</tr>
<tr>
<td>
<input type="text" name="user_id" value=<%=user.getUser_id()%>>
</td>
<td>
<input type="text" name="user_name" value=<%=user.getUser_name()%>>
</td>
<td>
<input type="text" name="user_password" value=<%=user.getUser_password()%>>
</td>
<td>
<input type="text" name="user_age" value=<%=user.getUser_age()%>>
</td>
</tr>
</table>
<table class="mytable">
<tr>
<td>------<input type="submit"  value="确认修改" />
</td>
</tr>
<tr>
<td>------<a href="Login_user.jsp" target="_parent">返回首页</a>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import = "dao.UserDao" %>
<%@ page import = "entity.User" %>
<%@ page import = "util.DBConn" %>
<%@ page import = "java.util.Date" %>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<jsp:useBean id="add" class="entity.User"></jsp:useBean>
<jsp:useBean id="bdd" class="dao.UserDao"></jsp:useBean>
<jsp:useBean id="cdd" class="util.DBConn"></jsp:useBean>
<jsp:setProperty property="*" name="add"/>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>
插入数据
</title>
<style>
body{text-align:center}
</style>
</head>
<style type="text/css">
.mytable{
width:1080px;
height:100%;
margin:0 auto;
font-weight: 200;
text-align:center;
}
.mytable1{
font-weight:1000;
width:100%;
height:100%;
margin:0 auto;
text-align:center;
background-color:#ffffff;
}
.shuju{
height:20px;
font-size:20px;
}
</style>
<body >
<script type="text/javascript">
function onClick(){
var user_name1 = document.getElementById('user_name');
var user_password1 = document.getElementById('user_password');
//<!--得到document.getElementById(" ") 得到的是id 对应的一个对象-->
var user_name=user_name1.value;
var user_password=user_password1.value;
var len= user_password.length;
var len1= user_name.length;
if(len1!=0&&len>5&&len<17)
{//功能是测试指定的字符串文本等是否符合
alert( "注册成功,请确认返回"+"\n                    你的用户名为:"+user_name +"\n                    你的密码为:"
+ user_password+"\n"+"密码长度为:"+len);
}else{
alert('用户名或密码输入不合法,请重新输入');
}
}
</script>
<form action="Myadd.jsp" method="post">
<table  class="mytable" border="3">
<tr> <tr> <tr></tr>
</tr>
<tr>
<td colspan="3" style="background-color:#ACD6FF;font-size:10px;height:10px;text-align:center;color:aliceblue">
<h1>注册界面</h1>
</td>
</tr>
<td valign="middle">用户名:<input type="text" name="user_name" id="user_name" style="height:30px;font-size:30px;" /></td>
<tr>
<td valign="middle">密码:<input type="password" name="user_password" id ="user_password" style="height:30px;font-size:30px;"/></td>
</tr>
<tr>
<td valign="middle">年龄:<input type="text" name="user_age" id="user_age"style="height:30px;font-size:30px;" /></td>
</tr>
<%
request.setCharacterEncoding("gb2312");
//获取姓名密码和年龄
String user_id="";
String user_name="";
String user_password="";
String user_age="";
if(!(request.getParameter("user_name")==null&&request.getParameter("user_password")==null&&request.getParameter("user_age")==null)){
user_name=request.getParameter("user_name");
user_name=new String(user_name.getBytes("ISO-8859-1"),"utf-8");
//request.setCharacterEncoding("GB2312");
user_password=request.getParameter("user_password");
user_password=new String(user_password.getBytes("ISO-8859-1"),"utf-8");
user_age=request.getParameter("user_age");
user_age=new String(user_age.getBytes("ISO-8859-1"),"utf-8");
}
User user=new User();
if(user_password==null || user_name==null ||user_password.length()<6 ||user_password.length()>16 ||user_password=="" ||user_name=="")
{
user_id="";
user_name="";
user_password="";
user_age="";
user.setUser_id(user_id);
user.setUser_name(user_name);
user.setUser_password(user_password);
user.setUser_age(user_age);
}
else{
Date date = new Date();
long time = date.getTime();
int a = (int)(Math.random()*1000);
String userId= String.valueOf(time);
user_id = userId + String.valueOf(a);
user.setUser_id(user_id);
user.setUser_name(user_name);
user.setUser_password(user_password);
user.setUser_age(user_age);
System.out.println(user.getUser_name()+"\n"+user.getUser_password()+"\n"+user.getUser_age());
try {
Connection conn = null;
UserDao user1=new UserDao();
DBConn obj=new DBConn();
conn=obj.getConnection();
System.out.println("Connection Successful!");
String sql="insert into Login(user_id,user_name,user_password,user_age) values(?,?,?,?)";
PreparedStatement pstmt=conn.prepareStatement(sql);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1,user.getUser_id());
pstmt.setString(2,user.getUser_name());
pstmt.setString(3, user.getUser_password());
pstmt.setString(4, user.getUser_age());
pstmt.executeUpdate();
System.out.println("插入成功");
} catch (Exception e) {
e.printStackTrace();
}
}
%>
</table>
<table class="mytable">
<tr>
<td>
<input type="submit" value="立即注册" onClick="onClick()"/>
</td>
</tr>
<tr>
<td>
<a href="login.html" target="_parent">返回</a>
</td>
</tr>
<tr>
<td>
<a href="Login_user.jsp" target="_parent">查看注册账号</a>
</td>
</tr>
</table>
</form>
</body>
</html>

已注册界面:
在这里插入图片描述

在这里插入图片描述

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>错误</title>
</head>
<body>
登录失败!!!
<a href="login.html" target="_parent">返回</a>
</body>
</html>

5.实验展示:
查看已注册用户:
在这里插入图片描述

用户注册页面:Myadd.jsp

登录失败界面:
在这里插入图片描述

4.编写配置文件:web.xml

注册界面:
在这里插入图片描述

Servlet层:login.java change.java delete.java
登录层:login.java

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>servlet.login</servlet-class>
<init-param>
<param-name>ouserName</param-name>
<param-value>小天</param-value>
</init-param>
<init-param>
<param-name>ouserPwd</param-name>
<param-value>123456</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>change</servlet-name>
<servlet-class>servlet.change</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>change</servlet-name>
<url-pattern>/change</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>delete</servlet-name>
<servlet-class>servlet.delete</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>delete</servlet-name>
<url-pattern>/delete</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
</web-app>

Web实验-Servlet控制层设计

修改界面:

<!DOCTYPE html>
<html>
<style type="text/css">
.mytable{
width:1080px;
height:100%;
margin:0 auto;
font-weight: 200;
text-align:center;
}
</style>
<body>
<form action="login" method="post">
<table  class="mytable" border="3">
<tr>
<td colspan="3" style="background-color:#ACD6FF;font-size:10px;height:10px;text-align:center;color:aliceblue">
<h1>登录界面</h1>
</td>
</tr>
<tr>
<td valign="middle">用户名:<input type="text" name="user_name" id="user_name" style="height:30px;font-size:30px;" /></td>
</tr>
<tr>
<td valign="middle">密码:<input type="password" name="user_password" id ="user_password" style="height:30px;font-size:30px;"/></td>
</tr>
</table>
<table class="mytable">
<tr>
<td>
<input type="submit" value="立即登录"/>
</td>
</tr>
<tr>
<td>
<a href="Myadd.jsp" target="_parent">立即注册</a>
</td>
</tr>
<tr>
<td>
<a href="Login_user.jsp" target="_parent">已注册账户</a>
</td>
</tr>
</table>
</form>
</body>
</html>

本文转自 https://blog.csdn.net/Tian208/article/details/121648368

腾讯云618
云惠网小编
SpringCloud -- Config、Bus解析 java

SpringCloud — Config、Bus解析

1、Config1.1、概述简介1. 分布式面临的问题微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要...
Java数据结构-了解复杂度 java

Java数据结构-了解复杂度

2.实例分析与计算  四.写在最后  // 计算斐波那契递归fibonacci的时间复杂度 int fibonacci(int N) { return N < 2 ? N : fibonacci...
Java数据结构-认识顺序表 java

Java数据结构-认识顺序表

目录二.顺序表1.概念及结构2.顺序表的实现打印顺序表获取顺序表的有效长度在pos位置新增元素判断是否包含某个元素查找某个元素对应的位置获取/查找pos位置的元素给pos位置的元素...
腾讯云618

发表评论