• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 160
  • Last Modified:

Servlet - accpeting request parameters

I am trying to pull a parameter off of a url to use it inside a query.  How can I refrence this inside the servlet?
0
programmer13
Asked:
programmer13
  • 4
1 Solution
 
TimYatesCommented:
httpServletRequest.getParameter( "Nameofparameter" ) ;
0
 
TimYatesCommented:
assuming you're inside a doGet() or doPut()
0
 
TimYatesCommented:
say your servlet got called by :

http://localhost/servlets/MyDaftServlet?woo=yay&foo=bar

then if inside your doGet(), you had:

--------------

Object o = httpServletRequest.getParameter( "woo" ) ;
if( o == null )
  System.out.println( "NO WOO!!" ) ;
else
  System.out.println( (String)o ) ;

--------------

The servlet would print out "yay"

:-)

Is this what you meant?
0
 
TimYatesCommented:
Hehehe...more detail (as you posted so many points, and I'm bored at work ;-)

------------------

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class MyDaftServlet extends HttpServlet {
  public void doGet(HttpServletRequest request,                    HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.print( "<HTML><BODY>");
    out.print( request.getParameter("woo") ) ;
    out.print( "</BODY></HTML>");
  }

  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }
}

--------------------------------------

Then

http://localhost/servlets/MyDaftServlet?woo=yay&foo=bar

would give you an HTML page with "yay" on it :-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now