【问题标题】:Create a Dynamic Front end JSP page for a Servlet , And also displaying the output of the Servlet to the JSP page为一个 Servlet 创建一个动态前端 JSP 页面,同时将 Servlet 的输出显示到 JSP 页面
【发布时间】:2014-06-24 13:47:15
【问题描述】:

假设我有一个 JSP 页面 index.jsp

<form action="https://localhost:8080/App/backend_servlet" method="get">
    <ul>
        <li>
            <label for="name"><b>User id:</b></label>
            <input type="text" name="userName" id="userName" size="40" title="Please enter Clients User id" ></input>
        </li>
        <li>
            <label for="name"><b>Password</b></label>
            <input type="text" name="pass" id="pass" title="Please enter Password."></input>
        </li>
        <!-- For the next input tag in the value part it should display the "String output" from the backend_Servlet-->
        <li>
            <label for="name"><b>Output</b></label>
            <input type="text" name="output" id="output" value = "TODAY" title="it should display output as reponse"></input>
        </li>                
    <p>
        <button type="submit" class="action">Submit</button>

    </p>
</form>

所以上面的 .jsp 页面包含一个向用户请求 用户名密码 的表单。因此,在用户输入他的用户名和密码并单击提交页面后,将进入处理请求的 backend_servlet 必须重定向到同一页面并必须显示 字符串输出 在页面的输出字段中。

public class backend_servlet extends HttpServlet implements SingleThreadModel
{  
// protected static ILogDevice m_oLogDevice;    

protected void doGet( HttpServletRequest request, HttpServletResponse response) 
    throws ServletException, IOException
{
    String ls_userName = request.getParameter("userName");
    String ls_pwd = request.getParameter("pass");
    String output = "";

    //Some Code which processes the userName and pwd and gives some value for the output string.                

    output = "Something";

}
}

有什么方法可以重写 index.jsp 的代码,以便它可以显示 servlet 的响应字符串。

不使用 servlet 中的 PrintWriterresponse.getWriter() 并重定向回其他一些 jsp 页面。

【问题讨论】:

标签: java html css jsp servlets


【解决方案1】:

对您的 JSP 代码进行一些修改,如下所示。使用JSP Standard Tag Library 或JSP 表达式语言访问请求属性。

要遵循的步骤:

  • index.jsp 首次加载时,responseString 将是 null,JSP 上不会显示任何内容
  • 现在将表单提交到 Servlet 后,根据用户身份验证将 responseString 设置为请求属性,并将请求重定向到 index.jsp 页面。
  • 重定向后responseString 将不再是index.jsp 中的null,而是显示在页面上。

JSP:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<form action="https://localhost:8080/App/backend_servlet" method="get">
    ...
</form>

<c:if test="${not empty responseString}">
    <c:out value="${responseString}"></c:out>
</c:if>

小服务程序:

request.setAttribute("responseString", output);
RequestDispatcher view = request.getRequestDispatcher("index.jsp");
view.forward(request, response);

您也可以根据范围访问它:

<c:out value="${requestScope.responseString}"></c:out>

编辑

根据您的代码直接在title 属性中设置它,如下所示。

<input type="text" name="output" id="output" 
                   value = "TODAY" title="${responseString}"></input>

【讨论】:

  • 谢谢,我如何使用这部分代码中的输出&lt;input type="text" name="output" id="output" value = "TODAY" title="it should display output as reponse"&gt;&lt;/input&gt;
  • 在我的帖子中更新了它。这很简单。如果responseStringnull,那么它将标题设置为空白,并且在重定向的情况下,它将设置实际标题。
  • _ HTTP 状态 500 - /index.jsp(23,8) "${responseString ! = null}" 包含无效表达式:javax.el.E​​LException:无法解析表达式 [ ${响应字符串! = null}] _ 请让我知道为什么它会显示此错误,如果您想要代码 sn-p,请告诉我
  • 只需尝试最后一个选项。无需进行null 检查。由EL自动处理。如果是null,那么它什么也不打印(空白字符串)。
  • 我需要在 Servlet 上执行一系列功能,例如连接、发送消息、注销。所以,这需要记住会话。但是当我尝试访问 session.getattribute 它返回空指针异常。您能否建议一些方法来存储会话详细信息。
猜你喜欢
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
  • 2013-12-13
  • 1970-01-01
相关资源
最近更新 更多