?
Solved

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

Posted on 2008-10-24
5
Medium Priority
?
798 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
[X]
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
  • 3
  • 2
5 Comments
 
LVL 60

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 60

Accepted Solution

by:
Kevin Cross earned 1200 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 60

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 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