Solved

How can I use HttpServlet?

Posted on 2002-07-06
7
165 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
Comment Utility
> 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
Comment Utility
0
 

Author Comment

by:freesky
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 92

Expert Comment

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

Expert Comment

by:manshan
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Passing list of object to Oracle Database Procedure 3 40
word0 challenge 4 52
Modeling a class in java 5 31
@SBGen Method 3 25
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now