3

jspフォームからデータを送信し、サーブレットを呼び出して、そのデータをサーブレットに表示することを試みています。

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の使用方法がわかりません。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

4 に答える 4

4

これは機能するはずです:String value =(String)req.getSession(false).getAttribute( "MasjidLocation")

于 2012-04-26T02:23:08.067 に答える
3

スクリプトレットは使用しないでください。それは1999年のスタイルです。JSTLを学び、それを使用してJSPを作成します。

サーブレットにHTMLが埋め込まれることは決してありません。パラメータを検証してバインドし、処理のためにサービスに渡し、JSPが表示するリクエストまたはセッションスコープに応答オブジェクトを配置するだけです。

于 2012-04-26T02:18:34.073 に答える
2

私はあなたが新しい技術について学ぶべきであるというduffymoに同意します(これが当てはまるなら、多分あなたのクライアントはそれを許すことができないでしょう...)。とにかく、属性の値を取得するには、次のようにします。

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

また、HTML<form>タグにサーブレットの2つの異なるパスがあることに気付きました。

MyWebArchive/MulitProcessorServlet

Week05WebArchive/MulitProcessorServlet

期待されますか?

于 2012-04-26T02:21:08.597 に答える
2

リクエストではなくセッションを使用しました。リクエストからセッションを取得する必要がある場合があります。

String strMasjidLocation = request.getSession().getAttribute("MasjidLocation");
于 2012-04-26T02:25:56.223 に答える