In the area of web application request & response are two major keys.The property of HTTP will remains universal over any programming language & platform, but here i mainly focus on Java/J2EE development.It means the response are related to each other.Normally the request is sent by client machine( browser ) and the response is revert back (sent back) to the client .Again for the response we need a clear idea about the content type ( MIME Type, Please read MIME TYPE in other post for more about content Type).
Key Points to remember about GET-
1. It has no Body, where as POST has Body.
2. It is Idempotent.
3. It is the default method in HttpServlet.In the Http servlet life cycle it is the default method.i.e. doGet().
4. It is not secure, because the data send by request line (URL) will visible on the rowser.
5. The amount of data for send with request using get method is very limited.
Below a HTML form like :-
<form action="servlet/MyServletTest" name="frmMyServlet" method="get">
<input type="text" name="txtValue" />
<input type="submit" value="Send Value" onclick="FunSendValue()"/>
</form>
Note-
If method name will not mentioned then bydefault it will take GET method as default.But be aware about your servlet , there must be a doGet() method for your operation/request you are sending. The doGet() must be there inside your resource servlet.If no doGet() method found then it will generate an error - HTTP method GET is not Supported.
The doGet() method inside the servlet like below :-
MyServletTest.java
public class MyServletTest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String paramVal=request.getParameter("txtValue");
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println("<h3> Your Value from JSP="+paramVal+"</h3>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
Like doGet() , there is doPost(),doPut(),doTrace(),doDelete() method for HTTP Servlet.The doGet() and doPost() methods are call by server ( via service method) for handling HTTP GET and POST requests.By extending HttpServlet ,here i have overrides the doGet() method for handle the request with GET method.
One more things to know is all Links,URL are the type GET in nature.
By default all links are handled by GET Method.Example :-
<a href="http://java.sun.com/j2ee?myval=I am Testing">Click here for
GET method Request</a> is a link with request type GET.
OR
<a href="servlet/MyServletTest?txtValue=I am a Programmer">Click
here for GET method Request</a>
Note-
The term Idempotent ! It means GET can safely repeated.No need to change the request link but keep in mind that the HTTP GET and servlet doGet() methods are quite different.Let me clear that HTTP GET is
idempotent as per HTTP 1.1 Specification but servlet doGet() method is non-idempotent. It means you repeate the link again and again without changes inside servlet doGet() method, then it may generate error like "Bad Request".And one thing about idempotent is that , it does not mean that the same request has always same response/output and we donot mean that request has no side-effect.
Read more from sun/oracle
Key Points to remember about GET-
1. It has no Body, where as POST has Body.
2. It is Idempotent.
3. It is the default method in HttpServlet.In the Http servlet life cycle it is the default method.i.e. doGet().
4. It is not secure, because the data send by request line (URL) will visible on the rowser.
5. The amount of data for send with request using get method is very limited.
Below a HTML form like :-
<form action="servlet/MyServletTest" name="frmMyServlet" method="get">
<input type="text" name="txtValue" />
<input type="submit" value="Send Value" onclick="FunSendValue()"/>
</form>
Note-
If method name will not mentioned then bydefault it will take GET method as default.But be aware about your servlet , there must be a doGet() method for your operation/request you are sending. The doGet() must be there inside your resource servlet.If no doGet() method found then it will generate an error - HTTP method GET is not Supported.
The doGet() method inside the servlet like below :-
MyServletTest.java
public class MyServletTest extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String paramVal=request.getParameter("txtValue");
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println("<h3> Your Value from JSP="+paramVal+"</h3>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
Like doGet() , there is doPost(),doPut(),doTrace(),doDelete() method for HTTP Servlet.The doGet() and doPost() methods are call by server ( via service method) for handling HTTP GET and POST requests.By extending HttpServlet ,here i have overrides the doGet() method for handle the request with GET method.
One more things to know is all Links,URL are the type GET in nature.
By default all links are handled by GET Method.Example :-
<a href="http://java.sun.com/j2ee?myval=I am Testing">Click here for
GET method Request</a> is a link with request type GET.
OR
<a href="servlet/MyServletTest?txtValue=I am a Programmer">Click
here for GET method Request</a>
Note-
The term Idempotent ! It means GET can safely repeated.No need to change the request link but keep in mind that the HTTP GET and servlet doGet() methods are quite different.Let me clear that HTTP GET is
idempotent as per HTTP 1.1 Specification but servlet doGet() method is non-idempotent. It means you repeate the link again and again without changes inside servlet doGet() method, then it may generate error like "Bad Request".And one thing about idempotent is that , it does not mean that the same request has always same response/output and we donot mean that request has no side-effect.
Read more from sun/oracle
No comments:
Post a Comment