JAVAWEB练习——完成简易用户登陆功能案例,完成文件下载

完成用户登陆功能案例

  1. 案例分析
    第一步:先做页面,再将数据提交到后台。
    第二步:后台即为Servlet,Servlet中包含常用的数据库处理

  2. 代码实现
    第一步:先做一个表单页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1 style="text-align: center;">简易登陆平台</h1>
	<div style="width:100%;text-align :center">
	<form action="/WEB13_UserLogin/LoginServlet"  method="post">
		用户名:<input type="text" name="username" placeholder="请输入用户名....."><br/>
		密码:<input type="password" name="password" placeholder="请输入密码..."><br/>
		<input type="submit" value="提交">
	</form>
	</div>
</body>
</html>

第二步:书写JDBCUtils

public class JDBCUtils {
    
    
  private static Connection conn;

  static {
    
    
	  try {
    
    
		Class.forName("com.mysql.jdbc.Driver");
		String url="jdbc:mysql:///web13";
		String username="root";
		String password="123";
		conn=DriverManager.getConnection(url,username,password);
	} catch (Exception e) {
    
    
		e.printStackTrace();
	}
	  
  }
  public static Connection getConnection() {
    
    
	  return conn;
  }
  public static void close(Connection connection,ResultSet resultSet,PreparedStatement preparedStatemnt) {
    
    
		if ( connection !=null) {
    
    
			try {
    
    
				connection.close();
			} catch (SQLException e) {
    
    
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		if ( resultSet !=null) {
    
    
			try {
    
    
				resultSet.close();
			} catch (SQLException e) {
    
    
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		if ( preparedStatemnt !=null) {
    
    
			try {
    
    
				preparedStatemnt.close();
			} catch (SQLException e) {
    
    
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}
	public static void close(Connection connection,Statement statement) {
    
    
		if ( connection !=null) {
    
    
			try {
    
    
				connection.close();
			} catch (SQLException e) {
    
    
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		if ( statement !=null) {
    
    
			try {
    
    
				statement.close();
			} catch (SQLException e) {
    
    
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}
}

第三步:创建一个叫web13的数据库,并在里面建一张表,取名users
在这里插入图片描述
第四步:书写Servlet的内容

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    
    
	public static Connection connection =JDBCUtils.getConnection();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		response.setContentType("text/html;charset=utf-8");
		String username =request.getParameter("username");
		String password =request.getParameter("password");
		String sql ="SELECT * FROM users WHERE username='"+username+"'and password ='"+password+"'";
		try {
    
    
			Statement st =connection.createStatement();
			ResultSet rs= st.executeQuery(sql);
			if (rs.next()) {
    
    
				response.getWriter().write("登陆成功");
			}else {
    
    
				response.getWriter().write("登陆失败");
			}
			
		} catch (SQLException e) {
    
    		
			response.getWriter().write("登陆失败");
			e.printStackTrace();
		}
		
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
		doGet(request, response);
	}

}

完成文件下载

分析:
文件下载的实质就是文件拷贝,将文件从服务器端拷贝到浏览器端。所以文件下载需要IO技术将服务器端的文件使用InputStream读取到,在使用ServletOutputStream写到response缓冲区

  1. 问题:什么情况下会文件下载??
    浏览器不能解析的文件就下载
  2. 问题:什么情况下需要在服务器端编写文件下载的代码?
    理论上,浏览器可以解析代码需要编写文件下载代码。实际开发中,只要是下载的文件都编写文件下载代码

先写一个html文档

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件下载</title>
</head>
<body>
<body>
        <h1>使用a标签直接指向服务器上的资源</h1>
        <a href="download/a.flv">a.flv</a><br/>
        <a href="download/a.mp4">a.mp4</a><br/>
        <a href="download/a.mp3">a.mp3</a><br/>
        <a href="download/a.jpg">a.jpg</a><br/>
        <a href="download/a.txt">a.txt</a><br/>
        <a href="download/a.zip">a.zip</a><br/>
        <h1>使用服务器端编码方式实现文件下载</h1>
        <a href="/DownloadServlet?filename=a.flv">a.flv</a><br/>
        <a href="/DownloadServlet?filename=a.mp4">a.mp4</a><br/>
        <a href="/DownloadServlet?filename=a.mp3">a.mp3</a><br/>
        <a href="/DownloadServlet?filename=a.jpg">a.jpg</a><br/>
        <a href="/DownloadServlet?filename=a.txt">a.txt</a><br/>
        <a href="/DownloadServlet?filename=a.zip">a.zip</a><br/>
</body>

</body>
</html>
public class DownloadServlet extends HttpServlet {
    
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        //获得需要下载的文件名称
        String filename = request.getParameter("filename");
        //要下载的这个文件的类型-------客户端通过文件的MIME类型区别文件类型
        response.setContentType(this.getServletContext().getMimeType(filename));
        //告诉客户端该文件不是直接解析而是以附件形式打开(下载)
        response.setHeader("Content-Disposition","attachment;filename="+filename);
        //获取文件的绝对路径
        String path = this.getServletContext().getRealPath("download"+filename);
        //获得该文件的输入流
        InputStream inputStream =new FileInputStream(path);
        //获得输出流--通过response获得的输出流用于向客户端写内容
        ServletOutputStream outputStream = response.getOutputStream();
        //文件拷贝的模板代码
        int len = 0;
        byte [] buffer =new byte[1024];
        while((len = inputStream.read(buffer))>0){
    
    
            outputStream.write(buffer,0,len);
        }
        inputStream.close();
        //outputStream.close();

    }

猜你喜欢

转载自blog.csdn.net/Mr_GYF/article/details/109160936