To send response to the http client, you need a reference to the output stream of the httpresponse. This is provided by the PrintWriter object obtained from the method PrintWriter getWriter() of the HttpServletResponse object as shown below and then call the println(String) method on it.
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
PrintWriter pWriter = resp.getWriter();
pWriter.println("It works.");
}
To set headers in the http response and check if specific headers are present, use the methods addHeader(..), addDateHeader(..), addIntHeader(..) and containsHeader(..) of the HttpServletResponse object.
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.addHeader("lang", "english");
resp.addDateHeader("createTime", Calendar.getInstance().getTimeInMillis());
resp.addIntHeader("iteration", 8);
resp.containsHeader("lang");
}
To redirect an httprequest to any other url, you need to use the method sendRedirect(String url) of the HttpServletResponse object where target url is the location where you want to redirect the request.
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.sendRedirect("/main/login.jsp");
}
To set the content encoding, content type and content length of the http response, you need to use the methods setCharacterEncoding(..), setContentLength(..) and setContentType(..) of the HttpServletResponse object.
These methods are essentially convenience methods for setting the corresponding headers Content-Encoding, Content-Length & Content-Type.
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setCharacterEncoding("utf-8");
resp.setContentLength(230);
resp.setContentType("text/html");
}
To set the response http status codes, you can use the methods setStatus(int), sendError(int) or sendError(int, String) of the HttpServletResponse object as shown below.
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setStatus(601);
//resp.sendError(602);
//resp.sendError(603, "Unauthorized access");
}
ıldır transfer
ReplyDeletegüzelbahçe transfer
foça transfer
mordoğan transfer
aliağa transfer
33Eİ1