How do you call a servelet from jsp?

  • Thread starter freshAtThis via JavaKB.com
  • Start date
F

freshAtThis via JavaKB.com

Please help.

I wrote a servelet that is to display a word document from data directory.
----------------------
public void doPost(HttpServletRequest reqst,
HttpServletResponse respns)
throws ServletException, IOException {}
-----------------------

here is my piece from the jsp that needs to call the servlet
I have a link, by selecting that link, I should be able to call the
servelet.
-------------------------
<a href="doPost('<%=fileName%>')"> File to View: <%= fileList%> </a> <br>
------------------------

Am i doing it correctly?
How would I launch the servelet to open, read and display the file?

Will doPost get called?

Please help - I need all the help I can get.

Thanks in advance.
 
R

Ryan Stewart

freshAtThis via JavaKB.com said:
Please help.

I wrote a servelet that is to display a word document from data directory.
----------------------
public void doPost(HttpServletRequest reqst,
HttpServletResponse respns)
throws ServletException, IOException {}
-----------------------

here is my piece from the jsp that needs to call the servlet
I have a link, by selecting that link, I should be able to call the
servelet.
-------------------------
<a href="doPost('<%=fileName%>')"> File to View: <%= fileList%> </a> <br>
------------------------

Am i doing it correctly?
How would I launch the servelet to open, read and display the file?

Will doPost get called?

Please help - I need all the help I can get.

Thanks in advance.

You're so far off it's almost funny :) You create a servlet, map that servlet to
a URL in web.xml, then link to that URL one way or another to send a request to
the servlet. Try here if you're running a J2EE 1.3 container:
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html

or here for 1.4:
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/Servlets.html
 
N

novice via JavaKB.com

is there a simple explanation?
I don't think I can go thru the whole tutorial - which requires downloading
code, compiling etc.

Please help!
any other suggestions???????
 
J

John C. Bollinger

novice said:
is there a simple explanation?
I don't think I can go thru the whole tutorial - which requires downloading
code, compiling etc.

If you are not prepared to learn the technology you are trying to use,
then you need to choose a different technology. I have no clue how you
expect to develop the code you want to write if downloading, modifying,
or compiling sample code is going to be a problem.

In any case, Ryan already gave you instructions for what you asked. If
you do not understand them then you need to learn the technology (-ies)
better, and the tutorials Ryan pointed you to are a good place to start.
 
R

Ryan Stewart

John C. Bollinger said:
If you are not prepared to learn the technology you are trying to use, then
you need to choose a different technology. I have no clue how you expect to
develop the code you want to write if downloading, modifying, or compiling
sample code is going to be a problem.

In any case, Ryan already gave you instructions for what you asked. If you do
not understand them then you need to learn the technology (-ies) better, and
the tutorials Ryan pointed you to are a good place to start.
Nevertheless, since I am feeling gracious, here is the standard form of a
servlet:
********* MyServlet.java
package foo;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
this.doService(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
this.doService(request, response);
}

public void doService(HttpServletRequest request, HttpServletResponse
response) throws IOException, ServletException {
PrintWriter out = response.getWriter();
out.write("Hello World");
}

}

To map it to a URL:
********** web.xml snippet
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>foo.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/someUrl</url-pattern>
</servlet-mapping>

Then http://localhost:8080/yourContext/someUrl will be your servlet. (Fill in
the correct port and context name obviously.)
 
N

novice via JavaKB.com

Ryan,

Thank you for answers. I did read up on servlets and defining the servlet
in web.xml

I have to say this, I was able to create a servlet (my very first) and got
it to compile and work. My servlet displays a file in the browser. The
problem is that it displays the file in the same browser that is calling it.

i want to open the file in a new browser.
How would I do that?
I have the following set:
response.setHeader("Content-Disposition", "inline; filename=test.pdf");
What parameters do I set to open the file in a new browser?

Thanks once again for all your help.
 
R

Ryan Stewart

novice via JavaKB.com said:
Ryan,

Thank you for answers. I did read up on servlets and defining the servlet
in web.xml

I have to say this, I was able to create a servlet (my very first) and got
it to compile and work. My servlet displays a file in the browser. The
problem is that it displays the file in the same browser that is calling it.

i want to open the file in a new browser.
How would I do that?
I have the following set:
response.setHeader("Content-Disposition", "inline; filename=test.pdf");
What parameters do I set to open the file in a new browser?

Thanks once again for all your help.
Try setting content disposition to "attachment; ..." Note that the
Content-Disposition header is not actually part of the HTTP standard, though it
is widely supported.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top