简体   繁体   English

jsp到servlet与URL的参数通信,而不是表单

[英]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. 我正在使用netbeans创建一个Web应用程序,允许手机用户扫描QR码并检索日志的上一个/下一个标题信息。 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. QR码将包含URL末尾的搜索字符串,我希望搜索在页面加载时开始,并输出包含搜索结果的页面。 For now (due to simplicity), my model is simply parsing an XML file of a small sample of MARC records. 就目前而言(由于简单),我的模型只是解析一小部分MARC记录的XML文件。 Oh, to top it off...I'm brand new to programming in java and using netbeans. 哦,最重要的是......我是java和使用netbeans编程的新手。 I have no clue about what javabeans are, or any of the more advance techniques. 我不清楚javabeans是什么,或任何更先进的技术。 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. 我创建了一个java类(main方法)来解析我的xml并正确地检索结果。 I have an index.jsp with my html in it. 我有一个带有html的index.jsp。 Within the index.jsp, I have no problem using get to get the title information from my URL. 在index.jsp中,使用get从我的URL获取标题信息没有问题。 But I have no clue how I would get those parameters to a servlet that contains my java code. 但我不知道如何将这些参数传递给包含我的java代码的servlet。 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. 如果我设法将搜索字符串提供给servlet,那么我不知道如何将该数据发送回index.jsp以在浏览器中显示。

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. 到目前为止,我见过的每个例子都假设您正在获取表单数据,但我需要在页面加载时找到,处理和返回参数... IOW,没有用户输入。

Thanks for any advice. 谢谢你的建议。 Ceci 塞西

You can get the url parameter in servlet using request.getParameter("paramName"); 您可以使用request.getParameter("paramName");获取servlet中的url参数request.getParameter("paramName");

and you can pass the attribute to page from servlet using forwarding request from servlet to jsp. 并且您可以使用从servlet到jsp的转发请求将该属性传递给servlet页面。

See Also 也可以看看

Just put the necessary preprocessing code in doGet() method of the servlet : 只需将必要的预处理代码放在servlet的 doGet()方法中:

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 eg 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/* . 并调用servlet的URL而不是JSP的URL,例如http://example.com/page?foo=bar而不是http://example.com/page.jsp?foo=bar与servlet映射相结合/page/*的URL模式。

Bear in mind that your JSP page will compile internally to a servlet. 请记住,您的JSP页面将在内部编译为servlet。 So you can retrieve the string and print it back in the same JSP. 因此,您可以检索字符串并将其打印在同一JSP中。 For instance assuming you get the string in a parameter called param you would have something like this in your JSP: 例如,假设您在名为param的参数中获取了字符串,那么您在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 最后一件事,你提到了main方法 - 只为独立应用程序执行 - 而你正在谈论web / JSP:O

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM