Thursday, 21 March 2013

Communication Between JSP and SERVLET

       In our previous Article we explained jsp elements. But with use of Jsp element We can not create whole project For that purpose we have to use Some Another feature of java web application And one of that is Servlet. In Introduction of Advanced java We explained the work of Servlet. But if you are new then you must have to know How we can do communication between jsp and servlet? So We decided to write an article on communication between jsp and Servlet.
       First thing is How to pass parameter from your jsp page to servlet?

PASS PARAMETER FROM JSP TO SERVLET.

         we can pass our parameters of form from one jsp to another jsp and also to a servlet. You can use  Given code for that purpose.
<form name=”f1” action=”communication”>
<label>name</label><input type = “text” value=”raj” name=”raj”><br>
<input type = “submit”  value = “SUBMIT”>
java
</form>     
By using this code you can Sent your value written in textbox named raj to the Servlet named Communication.java.
In Communication.java file you have to write this code in processrequest method
String myname=request.getparameter(“raj”);
out.println("my name is"+myname);

JSP TO SERVLET COMMUNICATION
          When you run this code you get one html with one text box.You have to write your article and click on submit and you will get output as your name will return in communication.java .Make sure that your button type is submit.Because by using this code all parameters of form is submitted to the page which is mentioned in action property of form tag.

PASS PARAMETER FROM SERVLET TO JSP 

            Above code is just explain you how to pass parameter from jsp page to servlet in real world we are not making this type of application instead of that we get parameter from jsp page do some process on that and send back it to the another jsp page. For this type of application you can use request.getdispatcher() method or you can also pass parameter with use of request.setattribute () method
Suppose you want to send your name to another jsp page from communication.java then follow this syntax in servlet file
request.setattribute(“name”,myname);
response.sendredirect(“next.jsp”);
And in next.jsp page
<%
String s=request.getattribute(“name”).toString();
%>
By using this code you can pass your name from servlet to jsp page.
You can also use response.getdispatcher() method for same thing for use of this method use given below code.
  RequestDispatcher send = request.getRequestDispatcher("next.jsp");  
  send.forward(requ,resp);
where requ and resp are object of http request and response.
So friends by using this two way you can send a value from servlet to jsp page.
      I know that this article is not teach you difficult part of web-application but using this you are able to solve difficult problems of communication between jsp and servlet page.If you found that this article is helpful to you then share with your friends too.Have a nice day. 

1 comment:

  1. Hello, and thanks for all the information you are providing me on Jsp and servlets.
    Also i I've worked a lot on FFT and use of Jitter to handle FFT data. But I didn't dive to deeply in Jitter, because I'm not really interested in images. What I'd like to do is to have the possibility to make some operations on matrix based on Symmetry applications of Group Theory,
    In a way to modify the spectral informations held in the matrix. Someone could argue if this could be done in Max, or could be better to work on it learning JAva and programming specific Externals? For that I am trying to go for 6 week training http://www.wiziq.com/course/12145-the-6-week-complete-java-primer-with-training-certificate wonder this course
    will help me out
    Thanks.

    ReplyDelete