Solved

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

Posted on 2008-10-24
5
781 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

11 Experts available now in Live!

Get 1:1 Help Now