GET方式传递表单数据需要注意的一个问题

本文详细探讨了在使用JSP进行网页开发时,利用request.getParameter()函数接收参数时容易遇到的陷阱,特别是当参数值为null时可能出现的问题,并通过两个具体示例说明了如何避免这些常见错误。

    在使用request.getParameter()这个函数接收其它页面传递过来的参数的时候需要特别注意的问题:

1.jsp

URL:http://localhost:8088/XX/1.jsp?param=

String param1 = request.getParameter("param");


    这种情况下param1 = null.  null==param1的结果是true。这还没有完,当我们继续将这个参数传个到下一个页面,看看会怎么样?

2.jsp

URL:http://localhost:8088/XX/1.jsp?param1=null

String param2 = request.getParameter("param1");

    这种情况下,param2==null和“”.equals(param2)均为false,为什么呢?实际上这里的param2=“null”,所以"null".equals(param2)输出为true。

     在开始学jsp时由于自己是个马大哈,经常疏忽这个问题,找半天才发现问题其实很简单,一直没时间记录下来,今天突然想起了将其记录下来以示自省,也给刚学jsp的菜鸟提个醒,高手勿视。

 

 

### 实现HTML表单数据同时提交至两个Servlet的方法 在Java Web开发中,HTML表单数据通常通过HTTP请求(GET或POST)提交到一个Servlet进行处理。然而,如果需要将表单数据同时提交到两个Servlet,可以通过以下几种方法实现。 #### 方法一:使用转发和重定向 在一个Servlet中接收表单数据后,可以将这些数据转发给另一个Servlet进行进一步处理。这种方式需要第一个Servlet将接收到的数据保存到请求属性中,并通过`RequestDispatcher`进行转发[^1]。 ```java protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取表单数据 String id = request.getParameter("id"); String pwd = request.getParameter("pwd"); // 将数据存储到请求属性中 request.setAttribute("id", id); request.setAttribute("pwd", pwd); // 转发到第二个Servlet RequestDispatcher dispatcher = request.getRequestDispatcher("/secondServlet"); dispatcher.forward(request, response); } ``` 这种方法适用于两个Servlet在同一Web应用中运行的情况。 #### 方法二:使用AJAX异步提交 通过JavaScript中的AJAX技术,可以分别向两个Servlet发送异步请求。这种方式不会刷新页面,用户体验更好[^5]。 ```javascript document.getElementById("myForm").addEventListener("submit", function(event) { event.preventDefault(); // 阻止默认提交行为 var formData = new FormData(this); // 向第一个Servlet发送数据 fetch("firstServlet", { method: "POST", body: formData }); // 向第二个Servlet发送数据 fetch("secondServlet", { method: "POST", body: formData }); }); ``` 这种方法适用于需要同时向多个后台服务提交数据的场景。 #### 方法三:使用过滤器(Filter) 通过配置一个过滤器,在过滤器中拦截请求并将表单数据复制一份发送给另一个Servlet[^2]。 ```java public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; // 获取表单数据 String id = httpRequest.getParameter("id"); String pwd = httpRequest.getParameter("pwd"); // 构造一个新的请求对象并转发给第二个Servlet RequestDispatcher dispatcher = httpRequest.getRequestDispatcher("/secondServlet"); dispatcher.forward(httpRequest, httpResponse); // 继续处理原始请求 chain.doFilter(request, response); } ``` 这种方法适用于需要对所有请求进行统一处理的场景。 #### 方法四:手动构造HTTP请求 在第一个Servlet中,通过`HttpClient`或其他类似的库手动构造HTTP请求,将表单数据发送给第二个Servlet[^4]。 ```java protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取表单数据 String id = request.getParameter("id"); String pwd = request.getParameter("pwd"); // 构造新的请求并发送给第二个Servlet HttpClient client = HttpClient.newHttpClient(); HttpRequest httpRequest = HttpRequest.newBuilder() .uri(URI.create("http://localhost:8080/secondServlet")) .header("Content-Type", "application/x-www-form-urlencoded") .POST(HttpRequest.BodyPublishers.ofString("id=" + id + "&pwd=" + pwd)) .build(); HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandlers.ofString()); } ``` 这种方法适用于需要跨域或跨应用提交数据的场景。 ### 注意事项 - 在使用上述方法时,需确保两个Servlet能够正确解析和处理接收到的数据。 - 如果使用AJAX异步提交,需要注意浏览器兼容性和错误处理逻辑。 - 在使用过滤器时,需合理设计过滤器的顺序和作用范围,避免影响其他功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值