1、首先创建一个登陆界面,代码如下:
package com.mx.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;
public class servlet_pro extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<h1>用户登录</h1>");
out.println("<form action='/hello/CLservlet' method='post'>");
//这是一个表单,action 就是把表单提交到什么地方,提交表单的过程就是向服务器发送
//请求的过程,其中 action 里面的书写方式为:/web 应用名/servlet 名
out.println("用户名<input type='text' name='username'/></br>");
out.println("密 码<input type='password' name='password'/></br>");
out.println("<input type='submit' value='登录'/></br>");
out.println("</form>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
2、创建登陆成功之后的跳转页面(下面这个是处理表单登陆程序,判断是否登陆成功):
package com.servletCL.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;
public class CLservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username=request.getParameter("username");
String password=request.getParameter("password");
out.println(username+" "+password);
//用户名和密码验证,如果成功,则跳转到下一个页面,一般吧常量写在 equals 的前面
//可以避免空字符串的问题,比如 username 如果为空,那么就没有equals 方法
if("maxiao".equals(username)&& "123".equals(password)){
// sendRedirect 函数的URL 写法:/web 应用名/servlet 名,后面的
//这个servlet 名 是要跳转到的 servlet
response.sendRedirect("/hello/MainFrame");
}else{
response.sendRedirect("/hello/servlet_pro");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
下面这个是登陆成功后的主界面:
package com.mx.view;
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;
public class MainFrame extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<h1>登录界面</h1>");
out.println("<body>");
out.println("<a href='/hello/Download_servlet?filename=girl.jpg'>点击下载图片</a>");
out.println("<a href='/hello/Download_servlet?filename=yinyue.mp3'>点击下载音乐</a>");
out.println("<img src='images/hulu.jpg' height='950' weight='350'>");
out.println("</body>");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
3、 创建下载链接程序:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Download_servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset = utf-8");
String filename = request.getParameter("filename");
if("girl.jpg".equals(filename)){
response.setHeader("content-Disposition", "attachment; filename=girl.jpg");
//其中 attachment 里面的 filename 的名字写的什么,那么在下载框里面就显示什么名字,是给下载用户看的
String path = this.getServletContext().getRealPath("/images/girl.jpg");
FileInputStream fis = new FileInputStream(path);
byte buff[] = new byte[1024];
int len = 0;
OutputStream os = response.getOutputStream();
while((len=fis.read(buff))>0){
os.write(buff, 0, len);
}
os.close();
fis.close();
}else if("yinyue.mp3".equals(filename)){
response.setHeader("content-Disposition", "attachment; filename=music.mp3");
String path = this.getServletContext().getRealPath("/music/yinyue.mp3");
FileInputStream fis = new FileInputStream(path);
byte buff[] = new byte[1024];
int len = 0;
OutputStream os = response.getOutputStream();
while((len=fis.read(buff))>0){
os.write(buff, 0, len);
}
os.close();
fis.close();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
整个servlet 项目结构如下图所示:
一般资源文件放在 WebRoot 文件夹下,比如 images、music 等提供下载的资源文件(放在其他文件夹会不成功)