Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse IDE - Cannot copy/paste from console output 8 127
GUI builder for Eclipse? 8 27
replace a word with other 1 40
Windows 10 and JDK 7 7u85 4 32
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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 about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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.

910 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

19 Experts available now in Live!

Get 1:1 Help Now