IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Web实验-Servlet控制层设计HTML-分层 -> 正文阅读

[Java知识库]Web实验-Servlet控制层设计HTML-分层

Web实验-Servlet控制层设计

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

实验步骤:
1.创建Web项目——WebServlet
在这里插入图片描述
2.源代码模块
在这里插入图片描述在这里插入图片描述
实验步骤:
数据库连接层:DBConn.java

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.	
	//}
}

数据库操作层:UserDao.java

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) {
			
		}
	}
}

实体类User.java

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;
	}
}

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

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='login.html' TARGET='_parent'>返回</A>");
			out.println("<BODY>");
		}
		out.println("</BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
		
	}
}

修改 chang.java

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();
			
	}
}


删除:delete.java

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();
 

	}
}	

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

<!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>

在这里插入图片描述

用户注册页面:Myadd.jsp

<%@ 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>

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

<%@ 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>

功能界面:Funtion.jsp

<%@ 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

<%@ 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>


错误:error.jsp

<%@ 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>

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

<?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>

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

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

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

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

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

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

修改界面:

在这里插入图片描述
在这里插入图片描述
删除界面:点击右侧按钮即可删除:
在这里插入图片描述

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-12-02 16:37:29  更:2021-12-02 16:38:20 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 4:18:43-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码