Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 45
hibernate jars 4 45
login form jsp example 2 37
Delphi Firemonkey: if the Sms contain special characters it won't send it 3 43
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

839 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