Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you call a servlet method from a jsp page?

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
Hi all,

I have troubles with trying to access a servlet being called from a jsp. If all goes well, then the servlet should write out to a file. but so far, there has been no file. if anyone could shed some light, that would be great.
thanks
sum
 
To access a servlet you need to send it an HTTP request in the form of either a GET or POST request (there are others like HEAD and DELETE but you wouldn't be explicitly using them in most circumstances). You could put a link (HREF) to the servlet and attach a querystring to the end to pass information. This is known as a GET request. The other option is point a form at the servlet. Example:
Code:
<form method=POST action=&quot;/servlet/HelloWorld&quot;>
<input type=submit>
</form>
The method for this type of request is specified in the FORM tag and can be either POST or GET. Use POST for requsests sending alot of data because alot of Web Server limit the Querystring of a GET request to 255 characters.

If you are already doing all of this then show us some code, otherwise we can't help you. Wushutwist
 
A JSP doesn't usually 'call' a servlet. Some typical interactions are to put links in your jsp that call a servlet when clicked on the resulting HTML page. If you are going to redirect, you would be better off using a servlet and having IT do a redirect rather than going to all the trouble of creating a jsp. JSP's should be used when you want to create a page with alot of content and don't want to have a bunch of println's. If you just need server side services, use a servlet instead.


It would probably help if you described the problem you are trying to solve a little more succinctly.
 
Hi all,

yes, I have done the <FORM .....>.... </FORM> with GET. I am trying to make it automatic though, so no one has to pushing submit button after redirect. i am thinking that i do need to be having it as a jsp because it generates the file. but instead of coming to the file, it goes to submit button. any ideas?

thanks again
sum
 
Your information is still a little vague...

Rather than explaining what you have done, why don't you describe the problem that you are trying to solve. For instance &quot;I have a page where I want the user to enter form data and when they submit the form data, I want it to be processed by a servlet and stored into a file&quot;. This will make it easier to help you find the best way to solve the problem.

If you want to see how a jsp talks to a servlet, go to my homepage:


when you click on the guestbook link it invokes a jsp that displays some dynamic data. When you enter guestbook information and click submit, it sends the form data to a servlet where it is processed and stored into a file. Also, if you click the read guestbook link, it calls the same servlet with different parameters.

I have a feeling that what you are trying to accomplish is similar. If this is so, I'll happily email you the source code so you can take a look.

Regards,

Charles
meadandale@hotmail.com
 
ok,
My goal is to provide a file with the data that is already stored in the servlet. All the user does is to push submit button, and then it should be nice to have that happening. problem is that when &quot;Submit&quot; pushed, I need to goto the servlet to process GET request. so the file is made, but how do you redirect to file then? i know where the file is. I hope this is sounding more clear.

thanks,
sum
 
This is still vague. Is it an html file? Text file? You can always use a RequestDispatcher to redirect the request to the other page. Example:
Code:
String destination = &quot;/file.html&quot;;
RequestDispatcher rd = request.getRequestDispatcher(destination);
rd.forward(request, response);
Where request and response are the HttpServletRequest and HttpServletResponse objects forwarded to your Servlet's doGet or doPost method. Wushutwist
 
A couple of things...

Forgive me if it is a result of the language barrier but

-- Servlets do not 'store' data.
-- You do not redirect to a 'file' unless it is another servlet, jsp or HTML page

From what you are telling me, you want to do the following:

1)user presses button sending an http request to your servlet
2)servlet reads information from a file and somehow presents it back to the user as a result

If this is what you are trying to do, here is a blurb of code that will do this:

// in your servlet
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType(&quot;text/html&quot;);
PrintWriter out = response.getWriter();
out.println(&quot;<html><head><title>A file</title></head>&quot;);
out.println(&quot;<body>&quot;);
//Easist way to do this
FileInputStream f = new FileInputStream(&quot;file.txt&quot;);
BufferedReader in
= new BufferedReader(new InputStreamReader(f);
String input = null;
while ((input = in.readLine()) != null)
out.println(input);

//done with file
out.println(&quot;</body></html>&quot;);
f.close();
}

As I said, I am doing something very similar for the guestbook app I wrote. I'll happily send you the source if you think it will help.

Charles


 
Forgive me for not being clear. It is all jumbled to you, but seems to be clear enough for me. The output file is a text file. I have a Java class that is making the text in CSV format. This what i want to eventually get to. The servlet works fine, but it is when I want to integrete the jsp page with the servlet the problems come. Meadandale your suggestion is good, but as i said, i have the file being written by the java class. also, the link on your site seems to be not working. Sorry for the problems in communication.

thanks,
sum
 
Then have the servlet display a link to download the file or kickoff the file download using javascript. I did a similar thing for a site I was working on about 6 months ago. Wushutwist
 
The campus is having network problems. Keep trying, it will eventually be resolved.

You still have not given us the problem you are trying to solve. You keep leaking out bits and pieces but none of it is sufficient to get the big picture view of what you are doing.

What is the jsp trying to do with the servlet? What is the servlet supposed to do? What's not working? Remember, a jsp IS a servlet. AFAIK a jsp can't call a servlet to get it to do work for it. It can redirect or forward an http request to a servlet but it can't say &quot;here, do this work and give me the results&quot;.

Until you provide more info or an email address so I can send you some code, I think we're just going to go round and round and get no where.
 
I think Meadandale is on the right track. I am sure there is a way to automate it but I am not sure what exactly IT is. If you mean automate the download of a file then yes. But the bigger question is how you percieve things happening. Maybe you should really explain your problem. Discribe the flow of events as you percieve them and then we might be able to really help you. Right now we are pretty much taking shots in the dark, making assumptions that may or may not be correct. Wushutwist
 
Hi all,

I follow your suggestions and now it looks to be working. My problem is that the servlet ran code to process the data passed to it, but it did not print anything out. so much thank yous to meadandale for your advice. also thank yous to wushutwist you make this clearer.

thanks,
sum
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top