Solved

How can I use HttpServlet?

Posted on 2002-07-06
7
168 Views
Last Modified: 2010-03-31
Hi,

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,
freesky



 












0
Comment
Question by:freesky
7 Comments
 
LVL 92

Expert Comment

by:objects
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 7134649
0
 

Author Comment

by:freesky
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,
freesky


0
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 
LVL 92

Expert Comment

by:objects
ID: 7136381
Sounds like it's not a servlet.
Why do you think it is a servlet?
0
 
LVL 1

Expert Comment

by:manshan
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 ****
<body>
<form name="test" method="post" action="MyServlet">
Enter Information
<input type="text" name="info" width="30">
<br>
<input type="submit" value="OK">
</form>
</body>

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)
{
   showUnit(req,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...
0
 
LVL 2

Accepted Solution

by:
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:

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

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.

Marius
0
 

Author Comment

by:freesky
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!

Freesky
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Timer (static) 9 43
xampp tool 12 50
Desingning Refactoring existing code 2 24
String array comparison 4 34
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

773 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