Introduction to Servlet technology
Chapters
Server Response
Some of the following method of HTTP Response header are:
Method & Description |
String encodeRedirectURL(String url)
|
String encodeURL(String url) Encodes specified URL by including session ID in it. |
boolean containsHeader(String name)
|
boolean isCommitted()
|
void addCookie(Cookie cookie)
|
void addDateHeader(String name, long date)
|
void addHeader(String name, String value)
|
void addIntHeader(String name, int value)
|
void flushBuffer()
|
void reset()
|
void resetBuffer()
|
void sendError(int sc)
|
void sendError(int sc, String msg)
|
void sendRedirect(String location)
|
void setBufferSize(int size)
|
void setCharacterEncoding(String charset)
|
void setContentLength(int len)
|
void setContentType(String type)
|
void setDateHeader(String name, long date)
|
void setHeader(String name, String value)
|
void setIntHeader(String name, int value)
|
void setLocale(Locale loc)
|
void setStatus(int sc)
|
More information can be found on tomcat's site. Here is the link
HTTP Header Response Example
The below is the HTTP header response example, which will return current time every 15 seconds with auto refresh.
import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class RefreshServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { refreshTask(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { refreshTask(request, response); } private void refreshTask(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.addHeader("Refresh", "10"); PrintWriter out = response.getWriter(); out.println(new Date()); } }
Result we get is a date shown in a browser window, browser refreshes ever every 10 seconds thus the date shown will be updated too.
In next part of this guide we will dive into HTTP status codes.
Description
This guide introduces Servlet technology and we will cover below topics
- What is a Servlet?
- Initial Setup
- Life Cycle
- Various Examples
- Client Request
- Server Response
- Http Status Codes
- Writing Filters
- Exception Handling
- Cookies Handling
- Session Tracking
- Database Access
- Packaging
- Internationalization
This is to the point introduction to the topic to get you started
Prerequisites
Working knowledge of Core Java is essential
Audience
Beginner to Java Servlet technology or students looking to brush up their skills quickly
Learning Objectives
Learn Java Servlets
Author: Subject Coach
Added on: 2nd May 2015
You must be logged in as Student to ask a Question.
None just yet!