Ceci
Ceci

Reputation: 13

jsp to servlet communication with parameters from URL, not a form

I'm using netbeans to create a web application that allows a cell phone user to scan a QR code and retrieve a journal's previous/next title information. The take home point is that there's no form that the user will input data into. The QR code will contain the search string at the end of the URL and I want the search to start on page load and output a page with the search results. For now (due to simplicity), my model is simply parsing an XML file of a small sample of MARC records. Oh, to top it off...I'm brand new to programming in java and using netbeans. I have no clue about what javabeans are, or any of the more advance techniques. So, with that background explanation here's my question.

I have created a java class (main method) that parses my xml and retrieves the results correctly. I have an index.jsp with my html in it. Within the index.jsp, I have no problem using get to get the title information from my URL. But I have no clue how I would get those parameters to a servlet that contains my java code. If I manage to get the search string to the servlet, then I have no idea how to send that data back to the index.jsp to display in the browser.

So far every example I've seen has assumed you're getting form data, but I need parameters found, processed and returned on page load...IOW, with no user input.

Thanks for any advice. Ceci

Upvotes: 1

Views: 7186

Answers (3)

BalusC
BalusC

Reputation: 1109635

Just put the necessary preprocessing code in doGet() method of the servlet:

String foo = request.getParameter("foo");
// ...
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);

And call the URL of the servlet instead of the JSP one e.g. http://example.com/page?foo=bar instead of http://example.com/page.jsp?foo=bar in combination with a servlet mapping on an URL pattern of /page/*.

Upvotes: 2

Jigar Joshi
Jigar Joshi

Reputation: 240966

You can get the url parameter in servlet using request.getParameter("paramName");

and you can pass the attribute to page from servlet using forwarding request from servlet to jsp.

See Also

Upvotes: 2

Liv
Liv

Reputation: 6124

Bear in mind that your JSP page will compile internally to a servlet. So you can retrieve the string and print it back in the same JSP. For instance assuming you get the string in a parameter called param you would have something like this in your JSP:

<%
 String param = request.getParameter( "param" );
 out.println( "String passed in was " + param );
%>

One last thing, you mentioned the main method -- that only gets executed for stand alone applications -- whereas you're talking web/JSP :O

Upvotes: 0

Related Questions