我正在尝试从 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();
}
}