2012-04-26 45 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");