Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving parameters in URL with Java

I have a little Servlet that uses XSL and XML to generate PDF. Since I want to specify the files via URL I need to get those Parameters from there:

localhost/Servlet?xml=c:\xml\test.xml&xsl=c:\xsl\test.xsl

so the parameters that I need are

 c:\xml\test.xml
 c:\xsl\test.xsl

and those need to be read into the variables xml-file and xsl-file.

I have this but that doesn't really help me I guess since I don't know how to apply the values into the variables:

Map para = request.getParameterMap();
java.util.Iterator it = params.keySet().iterator();

while ( it.hasNext() )
{
    String key = (String) it.next();
    String value = ((String[]) para.get( key ))[ 0 ];
}

Any idea on how to do that?

Thanks,

TheVagabond

like image 741
Thevagabond Avatar asked Nov 19 '12 11:11

Thevagabond


People also ask

How can I get parameters from a URL string?

The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions. Note: Page URL and the parameters are separated by the ? character. parse_url() Function: The parse_url() function is used to return the components of a URL by parsing it.

How do you query a URL in Java?

URL getQuery() method in Java with Examples The getQuery() function is a part of URL class. The function getQuery() returns the Query of a specified URL. Return Type: The function returns String Type Query of a specified URL.

How do you get parameters in Java?

To get all request parameters in java, we get all the request parameter names and store it in an Enumeration object. Our Enumeration object now contains all the parameter names of the request. We then iterate the enumeration and get the value of the request given the parameter name.

What is used to extract the query parameters from the URL?

Query parameters are extracted from the request URI query parameters, and are specified by using the javax. ws. rs. QueryParam annotation in the method parameter arguments.


2 Answers

In SERVLET must be request, yes?

String xml_path= request.getParameter("xml");

String xsl_path=request.getParameter("xsl");

like image 128
couatl Avatar answered Nov 02 '22 19:11

couatl


I think you simply want request.getParameter(String param)

e.g.

String xml = request.getParameter("xml");

Note (for future reference) that the above won't handle multiple xml parameters. For that you should use request.getParameterValues(String param)

As noted above you probably shouldn't be passing filenames around. In preference I would upload the file, generate the PDF and make that available (simply via the response, or perhaps store it local to your servlet deployment and return an id for later retrieval?)

like image 20
Brian Agnew Avatar answered Nov 02 '22 18:11

Brian Agnew