Solved

Can I see some sample code using HttpServletRequest to pick an "argument" off a URL (in Java)?

Posted on 2008-10-24
5
795 Views
Last Modified: 2013-11-24
I see from the doc on HttpServletRequest that it's an interface.  I need to know how to create an implementing object, call getParameter(), and return a String to the calling code.  The Java doc is not clear on how to use this: calling doGet() -- or which methods must be overridden.
0
Comment
Question by:dabeamer
  • 3
  • 2
5 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22798946
Hopefully you don't mind a little reading AND to save some re-typing, here is a sample of how a simple servlet implementation could look -- http:Q_23746028.html?#a22522353.

If you doGet and doPost are the same functionality, you will see a lot of times that there is a performAction method or something of that nature that has same method signature as doGet and doPost and both just call that method.

From the above, you now have the request object from the user (it is the parameter of the doGet method) and so you can do:

String variableName = request.getParameter("argument-name");

Using the response object, also passed as a parameter, you can open a PrintWriter to the calling client and print the value of variableName.

The above is the simple layout, for productional use you will need to account for null parameter (not in URL) or empty value (parameter exists, but has not value after =).

Hope this helps and is clear enough.  The link in the above EE question goes to Java site on Servlet Technology if you need further assistance or clarification.
0
 

Author Comment

by:dabeamer
ID: 22800097
Getting closer, but I think there are significant concepts I'm missing entirely...

I created an object based on HttpServlet, and put the  

String var = request.GetParameter("argtopass");

line in the doGet() method of the object.  My problem now is that I can't figure out how to "call it".  From reading thru the doc, I understand that this new object is running who-knows-how-or-where on the server.  I just want to poke it, and get back what's in the string.

All I want to do is read the parameter from the URL and store it in a string from a calling object/method.  As in  http://localhost:8080/appname.jsp?argtopass=lmnop
I just want to grab that "lmnop" and use it in a one-time database lookup right at the start of the app.  Then this new servlet can get destroyed as far as I'm concerned.

I'm guessing the answer lies either in figuring out session contexts (to find that other servlet process), or in the wsdl-related Annotations I read about somewhere.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 300 total points
ID: 22800444
??

If you are using a JSP page, you can use request.getParameter("argtopass") in there.  Noting that java is case sensitive, so GetParameter() is not the same as getParameter().

In the JSP page, you can use the value of the parameter if it exists in your database lookup right from there.  No need to have an additional servlet unless you don't want too; however, the servlet would be no different.  You would read the value of the parameter and then do the databsae query and as stated you can pass back data on PrintWriter.  If you are doing this servlet to servlet (essentially JSP page is another servlet), then you can use the:

ServletRequest API requestDispatcher()
http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/ServletRequest.html

RequestDispatcher include/forward()
http://java.sun.com/products/servlet/2.3/javadoc/javax/servlet/RequestDispatcher.html

These allow you to pass the request and response objects from one servlet to another, therefore, you could pass the request/response objects directly between the serlvets or include one page in another giving access to same request parameters and response to write to.

Hope that helps.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22800472
And one suggestion would be to make sure you work on one thing at a time to full understanding.  When learning new pieces of code.

Your question above had nothing to do with databases, it was just on reading a URL parameter and writting it back to the caller; therefore, please try implementing my suggestion in code and get that working.  

Once you do, you can then try to use the returned value in database.  This way you are building on each step.

Just my $0.02.

Regards,
Kevin
0
 

Author Closing Comment

by:dabeamer
ID: 31509768
Turns out I was asking the wrong question.  I need to know how the jsp piece of the puzzle works -- I was thinking everything could be done in the Java bean code.
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 241
use lov values 2 72
servlet example 17 59
JAVA API design with micro service cloud in mind 1 82
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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.

726 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