2012-04-26 81 views
3

我正在试验从jsp表单发送数据并调用servlet并在servlet中显示数据。使用jsp表单向servlet发送字符串 - 在servlet中使用jsp和getAttribute中的setAttribute

我想使用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(); 
} 
} 
+1

感谢大家的这种快速回复和帮助。 :-) – 2012-04-26 02:31:22

回答

4

这应该工作: 字符串值=(字符串)req.getSession(假).getAttribute( “MasjidLocation”)

3

请勿使用scriptlets;那是1999年的风格。学习JSTL并使用它编写您的JSP。

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

+1

感谢您的快速回复。至少现在我想证明我可以使用setAttribute和getAttribute,然后一旦工作,我可以移动到你的建议,所以我可以看到这可以做的所有不同的方式。 – 2012-04-26 02:22:35

2

我同意duffymo你应该学习更新的技术(如果这适用,也许你的客户不能这么做......)。总之,让你owuld做属性的值:

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

另外,我注意到你有你的HTML <形式为你的servlet两个不同的路径>标签:

MyWebArchive/MulitProcessorServlet 

Week05WebArchive/MulitProcessorServlet 

是否预计?

2

你用会话不请求。 您可能需要从请求中获取Session。

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