Java jstl中的http请求属性和参数,带一个servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18345416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
http request attributes and parameters in jstl, with a servlet
提问by CodeMed
I need to do some validation of numbers that come into a servlet1 from a web form. The process needs to look something like:
我需要对从 Web 表单进入 servlet1 的数字进行一些验证。该过程需要类似于:
1.) User sends two numbers (t1 and t2) to servlet1 from a web form
2.) Servlet 1 validates the numbers by checking: if (t2-t1)>2 , then t2 = t1 + 2
3.) The validated numbers are actually sent directly back to the jsp
4.) The end user's browser calls a different servlet2 with the output from this servlet1.
5.) The end user might repeat this process many times, producing many iterations of customized output
From my research, I think this means:
根据我的研究,我认为这意味着:
a.) setting attributes in servlet1 for t1 and t2 which are sent back to the user's browser with the results of the validation, so that
b.) numbers with a valid difference (<=2) can then be sent into servlet2.
My question has to do with how to manage the difference between parameters and attributes. I think the data starts as parameters in the html input textboxes. I have jstl code in the textboxes to make sure there is valid data. But my jstl just works with parameters. After the first use of the web form by the end user, I think all subsequent iterations will use attributes instead of parameters. Can anyone show me how to re-write the code that will manage the values of the attributes and parameters, so that the data is always what it should be?
我的问题与如何管理参数和属性之间的差异有关。我认为数据从 html 输入文本框中的参数开始。我在文本框中有 jstl 代码以确保有有效数据。但我的 jstl 只适用于参数。在最终用户第一次使用 web 表单后,我认为所有后续迭代都会使用属性而不是参数。谁能告诉我如何重写将管理属性和参数的值的代码,以便数据始终是它应该的样子?
So far, I have a jsp with an html form that includes input fields with the following format:
到目前为止,我有一个带有 html 表单的 jsp,其中包含具有以下格式的输入字段:
<input type="text" name="t1" value="${empty param.t1 ? '-1' : param.t1}" size="15" />
<input type="text" name="t2" value="${empty param.t2 ? '1' : param.t2}" size="15" />
The above jsp is the request dispatcher designated by a servlet1, whose doPost method includes something like the following:
上面的jsp是servlet1指定的请求调度器,它的doPost方法包含如下内容:
String t1 = request.getParameter("t1");
String t2 = request.getParameter("t2");
if(((double)t2-(double)t1)>2){
t2 = String.valueOf((double)t1 + 2);
}
request.setAttribute("t1", t1);
request.setAttribute("t2", t2);
How do I change this code to maintain the correct data values using attributes?
如何更改此代码以使用属性维护正确的数据值?
Never mind for now that I also need to check to see that the input is actually a number, etc.
现在没关系,我还需要检查输入是否实际上是一个数字等。
采纳答案by BevynQ
if you want the browser to do the request to Servlet2
the you can do this
如果您希望浏览器向Servlet2
您发出请求,您可以这样做
String t1 = request.getParameter("t1");
String t2 = request.getParameter("t2");
if(((double)t2-(double)t1)>2){
t2 = String.valueOf((double)t1 + 2);
}
response.sendRedirect(servlet2URL+"?t1="+t1+"&t2="+t2);
If it is ok for Servlet1
to call Servlet2
directly.
For this Servlet2
will need to use getAttribute
and not getParameter
as they may be different values.
如果可以直接Servlet1
打电话Servlet2
。因为这Servlet2
将需要使用getAttribute
而不是getParameter
因为它们可能是不同的值。
String t1 = request.getParameter("t1");
String t2 = request.getParameter("t2");
if(((double)t2-(double)t1)>2){
t2 = String.valueOf((double)t1 + 2);
}
request.setAttribute("t1", t1);
request.setAttribute("t2", t2);
RequestDispatcher requestDispatcher = request.getRequestDispatcher(servlet2URL);
requestDispatcher.forward(request,response);
option 3 dispatch jsp that includes call to servlet 2
选项 3 调度包含调用 servlet 2 的 jsp
String t1 = request.getParameter("t1");
String t2 = request.getParameter("t2");
if(((double)t2-(double)t1)>2){
t2 = String.valueOf((double)t1 + 2);
}
request.setAttribute("t1", t1);
request.setAttribute("t2", t2);
getServletContext().getRequestDispatcher("foo.jsp").forward
(req, res);
in foo.jsp
在 foo.jsp
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
t1 = <c:out value="${t1}"/>
t2 = <c:out value="${t1}"/>
<img src="url-pattern-for-servlet2">
...