【问题标题】:Using a jsp form to send a string to a servlet - using setAttribute in the jsp and getAttribute in the servlet使用jsp表单向servlet发送字符串——在jsp中使用setAttribute,在servlet中使用getAttribute
【发布时间】:2012-04-26 02:13:55
【问题描述】:

我正在尝试从 jsp 表单发送数据并调用 servlet 并在 servlet 中显示该数据。

我想使用 setAttribute 和 getAttribute。

在这个jsp文件中我使用了setAttribute:

<HTML>
<HEAD>
    <TITLE>
        Multi Processor
    </TITLE>
</HEAD>
<BODY>
    <h4>This is a form submitted via POST:</h4>
    <FORM action = "/MyWebArchive/MulitProcessorServlet" method = "POST">
        Enter your name: <INPUT type="TEXT" name="name"/>
        <BR/>
        <INPUT type="submit"/>
    </FORM> 
    <BR/>
    <h4>This is a form submitted via GET:</h4>
    <FORM action = "/Week05WebArchive/MulitProcessorServlet">
        Enter your name: <INPUT type="TEXT" name="name"/>
        <BR/>
        <INPUT type="submit"/>
    </FORM>
</BODY>
<%
String strMasjidLocation = "Selimiyie Masjid Methuen";
session.setAttribute("MasjidLocation", strMasjidLocation);
%>

</HTML>

这是我想使用 getAttribute 但我不知道如何使用 GetAttribute 的 servlet。你能告诉我我需要向 servlet 添加哪些额外代码,以便我可以从 setAttribute 中捕获值吗?

package temp22;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MulitProcessorServlet
 */
public class MulitProcessorServlet extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws IOException, ServletException {

    doPost(req, res);
}

public void doPost(HttpServletRequest req, HttpServletResponse res)
        throws IOException, ServletException {

    String name = req.getParameter("name");
    StringBuffer page = new StringBuffer();
    String methodWhoMadeTheCall = req.getMethod();
    String localeUsed = req.getLocale().toString();

    String strMasjidLocation = null;
    //strMasjidLocation = this is where I would like to capture the value from the jsp that called this servlet.

    page.append("<HTML><HEAD><TITLE>Multi Form</TITLE></HEAD>");
    page.append("<BODY>");
    page.append("Hello " + name + "!");
    page.append("<BR>");
    page.append("The method who called me is: " + methodWhoMadeTheCall);
    page.append("<BR>");
    page.append("The language used is: " + localeUsed);
    page.append("<BR>");
    page.append("I am at this location: " + strMasjidLocation);
    page.append("</BODY></HTML>");

    res.setContentType("text/html");
    PrintWriter writer = res.getWriter();
    writer.println(page.toString());
    writer.close();
}
}

【问题讨论】:

  • 感谢大家的快速回复和帮助。 :-)

标签: java jsp servlets setattribute getattribute


【解决方案1】:

这应该有效: 字符串值 = (String) req.getSession(false).getAttribute("MasjidLocation")

【讨论】:

    【解决方案2】:

    不要使用小脚本;那是1999年的风格。学习 JSTL 并使用它编写您的 JSP。

    您的 servlet 永远不应该在其中嵌入 HTML。只需验证和绑定参数,将它们传递给服务进行处理,然后将响应对象放在请求或会话范围内以供 JSP 显示。

    【讨论】:

    • 感谢您的快速回复。至少现在我想证明我可以使用 setAttribute 和 getAttribute 然后一旦它起作用了,我就可以继续你的建议,这样我就可以看到所有不同的方法可以做到这一点。
    【解决方案3】:

    我同意 duffymo 的观点,即您应该学习更新的技术(如果适用,也许您的客户不允许这样做......)。无论如何,要获得你应该做的属性的值:

    strMasjidLocation = (String)req.getSession().getAttribute("MasjidLocation");
    

    另外,我注意到您的 HTML

    标签中有两个不同的 servlet 路径:
    MyWebArchive/MulitProcessorServlet
    

    Week05WebArchive/MulitProcessorServlet
    

    这是预期的吗?

    【讨论】:

      【解决方案4】:

      您使用的是会话而不是请求。 您可能需要从请求中获取 Session。

      String strMasjidLocation = request.getSession().getAttribute("MasjidLocation");
      

      【讨论】:

        猜你喜欢
        • 2013-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多