使用jsp表单将字符串发送到servlet - 使用jsp中的setAttribute和servlet中的getAttribute

时间:2012-04-26 02:13:55

标签: java jsp servlets setattribute getattribute

我正在尝试从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的servlet,但我不知道如何使用GetAttribute。你能告诉我我需要添加到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();
}
}

4 个答案:

答案 0 :(得分:4)

这应该有效: String value =(String)req.getSession(false).getAttribute(“MasjidLocation”)

答案 1 :(得分:3)

不要使用scriptlet;这是1999年的风格。学习JSTL并使用它编写JSP。

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

答案 2 :(得分:2)

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

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

另外,我注意到HTML中的servlet有两种不同的路径&lt;形式&GT;标记:

MyWebArchive/MulitProcessorServlet

Week05WebArchive/MulitProcessorServlet

预期吗?

答案 3 :(得分:2)

您使用了Session not Request。 您可能需要从请求中获取会话。

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