Servlets are like regular java program but with peculiar characteristics. It is generally intended to run in web server and give web services to the clients. Servlets are made to handle the HTTP requests like GET and POST. These requests are handled by overriding the doGet() and doPost() methods in Service() method of javax.servlet.Servlet interface.
- Do not have main() method; runs in servlet container on web server like tomcat
- Implements javax.servlet.Servlet interface of J2EE
- Implements http requests like get, post, put, delete etc by overriding doGet(), doPost() methods of HttpServlet class
- doGet() and doPost() methods in servlet gets parameters from browser in HttpServletRequest and sets parameters of HttpServletResponse which can be used by JSP to display variables and render html in User interface
get request —————>|———————— | overrides
get response <———— | Servlet implements |<———>doGet()
| Service() method |
post request ————> | of Servlet class |<———>doPost()
post response <————|________________ |
Servlet Hello World Example
It is very easy to program servlet, we can program it only with the knowledge of which class to inherit and which methods to override.
getWriter() method of the HTTPServletResponse class to set the output to response object. In the last two lines, HTML output is printed, so that browser receiving that can render proper HTML.
include java.io.*
include javax.servlet.*
include javax.servlet.http.*
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException {
response.setContentType("text/html");
PrintWriter out=getWriter();
out.println("<html><head><title>Hello World</title></head>");
out.println("<body><h1>Hello World</h1>You are welcome!</body></html>");
}}
getWriter() method of the HTTPServletResponse class to set the output to response object. In the last two lines, HTML output is printed, so that browser receiving that can render proper HTML.
Two important interfaces included in servlet API
HttpServletRequest interface
When any request is made by client, the servlet container makes request object and passes to service() method of the servlet. This interface encapsulates functionality for a request object passed to HTTP servlet. Provides access to input stream and allows servlet to read data from clients like from forms. It contains following methodspublic Cookie[] getCookies() public String getQueryString() public HttpSession getSession() //creates session if no established session public HttpSession getSession(boolean create) //creates session if arg is set public String getHeader(String name) public String getParameter(String name)
HttpServetResponse interface
Encapsulates the functionality of response object returned to the client by the servlet. Provides access to output stream and allows servlet to send data to clients. It contains following methods to formulate response to clientpublic void addCookie(Cookie cookie); public void sendError(int statusCode) throws IOException public void sendError(int statusCode, String message) throws IOException public PrintWriter getWriter() //Outputs character based output data public ServletOutputStream getOutputStream() //outputs byte based binary data public void sendRedirect(String location) throws IOException //temp redirect response
No comments:
Post a Comment