0

我正在对 servlet 和 jsp 做一个小要求。

servlet 将包含变量 id、name、email、gender。有时这些值将为空。

有时变量的值为空。例如 id 和 name 包含值 1123 和 pratap。

response.setContentType("text/html;charset=UTF-8");
              try {
             //TODO output your page here
        RequestDispatcher view = request.getRequestDispatcher("registration.jsp");
    view.forward(request, response);
           request.setAttribute("id","value"); 
        } finally {            

        }

我的jsp页面

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
          <form method="GET" action='registration1'>
    <input type="text" name="address"/>
    <input type="text" name="phoneno"/>

    <input type="text" name="pincode" />
    <label>${id}</label>
    <input type="submit"/>
    </form>
        </body>
    </html>

这样控件将转到registration.jsp

在registration.jsp中,我应该获取电子邮件和性别的文本框,以及id和名称的文本框,我应该获取文本框中的值,以便用户无法更改这些值。(因为这些值已经填写并证明是正确的.)

对于上面的 jsp,我尝试使用 id,但我无法在 jsp 中看到 id 值。

如何将这些变量传递给 jsp 的文本框,并在值为 null 时提示输入值。

谢谢..

4

1 回答 1

1

您需要在 Servlet 中设置值作为请求属性并在 JSP 中获取它们。在 JSP 中获取它们后,相应地检查并启用/禁用表单控件。

小服务程序:

request.setAttribute("phoneno","9998386033");

JSP:

<%
String phoneno=null;
if(request.getAttribute("phoneno")!=null) 
    phoneno = request.getAttribute("phoneno").toString();
%>

<% if(phoneno!=null) {
     out.println("<INPUT TYPE=\"text\" name=\"phoneno\" value=\""+phoneno+"\" disabled=\"disabled\" ");
   } else {
       out.println("<INPUT TYPE=\"text\" name=\"phoneno\" ");
   }
%>

对于 JSP EL

<c:if test="${empty phoneno}">
    <INPUT TYPE="text" name="phoneno" value="${phoneno}" disabled="disabled"/>
</c:if>
<c:if test="${not empty phoneno}">
    <INPUT TYPE="text" name="phoneno"/>
</c:if>
于 2012-05-14T12:16:27.450 回答