HTTP/1.1 200 OK Content-Type: text/html Header2: ... ... HeaderN: ... (Blank Line) <!doctype ...> <html> <head>...</head> <body> ... </body> </html>
The status line comprises of the HTTP variant (HTTP/1.1 in the illustration), a status code (200 in the case), and a short message relating to the status code (OK in the case).
Methods | Description |
---|---|
String encodeRedirectURL(String url) | Encodes the predetermined URL for use in the sendRedirect method. |
boolean isCommitted() | It returns boolean when response is commited. |
void addCookie(Cookie cookie) | It is utilized to add the cookie to the response. |
void setContentType(String type) | To set the content type while sending to the client from the server. |
package servletresponse; import javax.servlet.http.*; import javax.servlet.*; import java.io.*; public class MyServletDemo extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html"); PrintWriter pwriter=res.getWriter(); String name=req.getParameter("uname"); pwriter.println("User Details Page:"); pwriter.println("Hello "+name); pwriter.close(); } }
The doGet() Method. A GET request results from an normal request for a URL or from a HTML web form that has no METHOD determined and it ought to be taken care of by doGet() technique.
web.xml
<web-app> <servlet> <servlet-name>DemoServlet</servlet-name> <servlet-class>servletresponse.MyServletDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>DemoServlet</servlet-name> <url-pattern>/mydetails</url-pattern> </servlet-mapping> </web-app>
Make sure that servlet name should be same as that is DemoServlet in both the tags. URL pattern should be match with below HTML page action form.
index.html
<form action="./mydetails" method="get"> User name: <input type="text" name="uname"> <input type="submit" value="login"> </form>
Here the developer just created the User name and submit button that is login.
Output
The following output is generated by compiling the above code.