提示:在Servelet的实现里可以调用HttpServletRequest的getSession(true)方法来创建session。
package zp.javaee;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="SimpleDownloadServlet" , urlPatterns="/SimpleDownloadServlet")
public class SimpleDownloadServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String fileToDownload = request.getParameter( "filename");
final int BYTES = 1024;
int length = 0;
ServletOutputStream outStream = response.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
response.setContentType( (context.getMimeType( fileToDownload ) != null) ?
context.getMimeType(fileToDownload) : "text/plain" );
response.setHeader( "Content-Disposition", "attachment; filename=\"" + fileToDownload + "\"" );
InputStream in = context.getResourceAsStream( "/" + fileToDownload);
byte[] bbuf = new byte[BYTES];
while ((in != null ) && ((length = in.read(bbuf)) != -1))
{
outStream.write(bbuf,0,length);
}
outStream.flush();
outStream.close();
}
}
将请求转接到别的Servlet
package zp.javaee;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="SimpleDispatcherServlet" , urlPatterns="/SimpleDispatcherServlet" )
public class SimpleDispatcherServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext sc = getServletConfig().getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher("/SimpleDownloadServlet" );
rd.forward(request, response);
}
}
重定向到外网
package zp.javaee;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="SimpleRedirectServlet", urlPatterns="/SimpleRedirectServlet")
public class SimpleRedirectServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect("");
}
}
Cookie
package zp.javaee;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet (name="SimpleWriteCookieServlet" , urlPatterns="/SimpleWriteCookieServlet" )
public class SimpleWriteCookieServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType( "text/html;charset=UTF-8" );
PrintWriter out = response.getWriter();
Cookie cookie = new Cookie("name" ,"zp" );
cookie.setHttpOnly( true );
cookie.setMaxAge(-30);
response.addCookie(cookie);
out.println( "Set CookieSet Cookie");
out.close();
}
}
package zp.javaee;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet (name="SimpleReadCookieServlet" , urlPatterns="/SimpleReadCookieServlet" )
public class SimpleReadCookieServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType( "text/html;charset=UTF-8" );
PrintWriter out = response.getWriter();
out.println( "Set Cookie" );
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
out.println( ""
+ cookie.getName() + " : " + cookie.getValue() + "");
out.println( "" );
}
out.close();
}
}
析构
覆盖servlet的destroy方法,在里面释放资源。