1

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

jmj
  • 237,923
  • 42
  • 401
  • 438
Ceci
  • 13
  • 1
  • 3

3 Answers3

2

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

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
2

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/*.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks! This makes sense. Editing the servlet as you suggested and calling the URL for the servlet instead of the JSP is "sort of" working. All I'm trying to do for now is a simple println in the servlet of the parameter it retrieved from the URL. IOW, my JSP loads after pointing my browser to the servlet URL+variable, but I don't see the parameter that my servlet retrieved from the URL, and I don't know how to place that result in a particular location on my JSP. I've seen some posts here discussing EL in a JSP, but those are for POST/form situations. – Ceci Apr 21 '11 at 18:19
  • Just use `${param.foo}` in JSP to get the parameter in JSP. It *really* doesn't matter if it is POST or GET. It only matters for the workflow in the servlet. See also http://stackoverflow.com/tags/servlets/info and this is probably also helpful http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – BalusC Apr 21 '11 at 18:22
  • Thanks! You've really helped. Worked like a charm – Ceci Apr 21 '11 at 18:41
0

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

Liv
  • 6,006
  • 1
  • 22
  • 29
  • 1
    Thanks for the quick response. That's exactly my problem. I have all my code as a main method...ergo why I'm considering the servlet. I have no problem retrieving my variable from the URL and keeping it contained within the JSP. I can output it to the page just fine. But I can't simply copy my java code to my jsp, so I have no way to start up my XML parser. I'm new enough to coding that teaching myself the XPath I needed for that was a case of blood, sweat and tears. So the idea of rewriting it in the 2 days I have left, is probably bordering on impossible. – Ceci Apr 21 '11 at 16:58
  • Not only that, using JSP *scriptlets* is also discouraged since a decade. See also http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – BalusC Apr 21 '11 at 17:05
  • Yeah, after doing some research, I discovered that it's poor form to put much code in a JSP. It makes sense to separate things. I'm just at a loss as to how to pass the data to my parsing program and then back to the JSP. I think I made an "architecture" mistake by creating the code as a main class. Heck, I don't even know how I'd copy to code into a servlet, much less move the data around between JSP and servlet. It appears that I dreamed up a project for my java course that was *well* beyond the skills of a new programming student. – Ceci Apr 21 '11 at 17:17