为什么在JSP中获得空值?

编程入门 行业动态 更新时间:2024-10-22 17:19:03
本文介绍了为什么在JSP中获得空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用Web表单提交一些信息,然后JSP页面将显示输入的表单信息.但是,当我单击表单中的提交"按钮时,它将进入正确的JSP文件,但是所有表单值都显示为空".我正在使用Jersey来执行POST请求.

I want to use a web form to submit some information and then a JSP page will display the entered form information. However when I clicked the submit button in the form, it goes the correct JSP file but all form values are displayed as "null". I'm using Jersey to do the POST request.

表格为:

<form action="/MyRestWS/rest/customer/created" method="POST"> <table border="1"> <tr> <td>Customer name:</td> <td><input type="text" name="name"></td> </tr> <tr> <td>Customer ID:</td> <td><input type="text" name="id"></td> </tr> <tr> <td>Customer DOB:</td> <td><input type="text" name="dob"></td> </tr> </table> <br/> <input type="submit" value="Submit"> </form>

执行请求的代码是:

@Path("/customer") public class CustomerService { @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Path("created") public Response createCustomer(@FormParam("id") int id, @FormParam("name") String name, @FormParam("dob") Date dob) { Response r; r = Response.ok().entity(new Viewable("/confirm.jsp")).build(); return r; } @GET @Produces(MediaType.TEXT_HTML) public Viewable displayForm() { return new Viewable("/form.html"); } }

显示的JSP文件为confirm.jsp,其内容为:

The JSP file displayed is confirm.jsp and its content is:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "www.w3/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Your entered information</title> </head> <body> <h2> <% out.println("You've entered the following information:"); %> </h2> <p> Customer Name: <%=request.getParameter("name")%></p> <p> Customer ID: <%=request.getParameter("id")%></p> <p> Customer DOB: <%=request.getParameter("dob")%></p> </body> </html>

如果我在浏览器中键入以下地址:

If I type the following address in a browser:

localhost:8080/MyRestWS/rest/customer

它将显示带有表单的form.html.填写信息并单击提交"后,它将转到以下地址并显示路径指定的JSP文件:

It will show me the form.html with the form. After I fill out information and click "Submit", it will go to the following address and display the JSP file as specified by the path:

localhost:8080/MyRestWS/rest/customer/created

JSP文件正确显示,但是所有客户信息字段都显示为"null",如下所示:

The JSP file shows up correctly but all customer information fields are displayed as "null" like the following:

You've entered the following information: Customer Name: null Customer ID: null Customer DOB: null

那么,为什么在提交表单后在JSP中得到空值?我的代码有什么问题?

So why do I get null values in JSP after submitting the form? What's wrong with my code?

推荐答案

您正试图显示不再存在的请求参数.它们仅在 first 请求(表单提交)期间存在.

You're trying to display request parameters that no longer exist; they exist only for the duration of the first request, the form submission.

如果要再次显示它们,则需要根据请求属性将它们公开到视图层.

If you want to display them again you need to expose them to the view layer as request attributes.

(也就是说,如果将它们作为构造类的一部分公开,我会更高兴.)

(That said, I'd be happier if they were exposed as part of a constructed class.)

更多推荐

为什么在JSP中获得空值?

本文发布于:2023-07-19 08:52:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1154897.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JSP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!