Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

How can I use HttpServlet?

Posted on 2002-07-06
Last Modified: 2010-03-31

I want to do a quite simple thing but I don't know exactly how.  I work with IBM Visual Age.

Here is what I want:
1 - The user enters information and then click on a button
2 - The "click" event calls my method with the information as parameters.
3 - My method execute a query
4 - And then my method shows the results of that query using jsp.

I want to use the concepts of "request" and "response" but I don't know exactly how.

What I have problem with is no. 2 (where I get the "request" and "response"?) and no. 4.

Here is my method' signature:

public void showUnit(HttpServletRequest request, HttpServletResponse response)

Thanks a lot for your time,


Question by:freesky
LVL 92

Expert Comment

ID: 7134332
> 2 - The "click" event calls my method with the
> information as parameters.

'How' is your method getting called?

Is this an application, an applet, a web page or what.

HttpServlet is used for constructing servlets, which can be called by web pages (similiar to cgi), and the request/response objects are passed to the servlet by the servlet container.
LVL 92

Expert Comment

ID: 7134649

Author Comment

ID: 7135859
Hello Objects,

My project is an application in which I have to implement a web page with the information mentionned earlier.

Your question "'How' is your method getting called?" is very good because it is exactly what I would like to know.
Once the user has clicked, where or how do I get the "request" and "response" parameters to send them to my method?

Once I get these parameters, I exactly know what to do with them.  My problem is "getting" them.

Thanks for your time,

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 92

Expert Comment

ID: 7136381
Sounds like it's not a servlet.
Why do you think it is a servlet?

Expert Comment

ID: 7136764
U can have a static HTML page for your user to enter information. Once the user has entered his information u can post this HTML page to a JSP page that will request these information and query the database and get the results.

U can write ur method in the JSP page itself. but thatz not a efficient way of doing it.

use java bean instead or post ur HTML page to a servlet and then your servlet query the database and get the results and display it or can forward the same to a JSP page for display.

the HTML page
**** am just giving the body part of the HTML here ****
**** displays a text control and a OK button ****
<form name="test" method="post" action="MyServlet">
Enter Information
<input type="text" name="info" width="30">
<input type="submit" value="OK">

once when the user enters the information n press OK this page will be submitted to ur servlet called MyServlet.

**** Servlet code ****
public void doPost(HTTPServletRequest req, HTTPServletResponse res)

**** in ur method ****
public void showUnit(HttpServletRequest request, HttpServletResponse response)    {

String info=request.getParameter("info");
//similar to the statement above get all ur other info
//that your user has entered

...... do ur database operations ......

...... display the results here .....


hope this helps...

Accepted Solution

xxMariusxx earned 100 total points
ID: 7139130
You have two systems here; the client and the server.  In manshan's code above, you see some HTML code:

<form name="test" method="post" action="MyServlet">
Enter Information
<input type="text" name="info" width="30">
<input type="submit" value="OK">

This renders the form interface in the user's browser.  When they click the submit ("OK") button, the browser takes the information they entered into the form and sends it to the location specified in the form's "action" attribute.  In this case, the action is "MyServlet"...so the browser will initiate a request to the server at URL "http://www.somedomain.com/MyServlet" and pass up the information that was entered into the form.

The HTTP Server running on somedomain.com will recognize the MyServlet URL as a servlet and pass the request on to the J2EE application server.  In your case, this would be Websphere...in VisualAge, the Websphere Test Environment is a hybrid of IBM Http Server (Apache basically) and a stripped down version of Websphere Application Server, which is what holds the Servlet Engine and EJB Container (your JVMs).  So that 'passing' from the HTTP Server to the App Server is pretty transparent.  

In any case, the Application Server sees the request and generates two objects...an HttpServletRequest and an HttpServletResponse.  It locates the MyServlet instance that exists in the Servlet Engine and calls the service() method on it, passing in those two objects.  service() analyzes the request 'METHOD' (GET, POST, etc) and calls the appropriate handling method.  In the case of this form, the request 'METHOD' was "post"...so service() will call doPost(), passing in the HttpServletRequest and HttpServletResponse objects.

All of this is transparent to you.  The only thing you've done up to this point is click on a button.  Your job in writing the servlet is to create a class that extends HttpServlet and overrides the doPost() method.

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   // insert your own code here

Inside of doPost(), you need to analyze the contents of the request to determine your course of action (assuming you're not using Struts) and then make the appropriate method call.

Finally, you use the HttpServletResponse object to send a response back to the browser.  Send the response data through the HttpServletResponse's output stream and the Application Server will pass the data to the HTTP Server, which will, in turn, pass the data back to the browser.


Author Comment

ID: 7170107
Thank you all for your answers, they were very useful to understand the concept of httpServlet.

Have a nice day and thanks again!


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
table example 4 32
by zero exception 10 52
Desingning Refactoring existing code 2 26
Java syntax, or is it Selenium 6 30
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question