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

Posted *骑鱼~过海*

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("跳

以上是关于Web实验-Servlet控制层设计HTML-分层的主要内容,如果未能解决你的问题,请参考以下文章

java Web之:分层设计

H5+app前端后台ajax交互总结

Spring MVC体系结构和处理请求控制器

ABP分层设计

web项目的分层开发

java里分的三层到底是哪三层?