Using a jsp form to send a string to a servlet - using setAttribute in the jsp and getAttribute in the servlet

StackOverflow https://stackoverflow.com/questions/10326348

質問

I'm experimenting with sending data from a jsp form and calling a servlet and showing that data in the servlet.

I would like to use setAttribute and getAttribute.

In this jsp file I'm using 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>

This is the servlet I would like to use getAttribute but I don't know how to use GetAttribute. Can you show me what additional code I need to add to the servlet so I can capture the value from the 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();
}
}
役に立ちましたか?

解決

This should work: String value = (String) req.getSession(false).getAttribute("MasjidLocation")

他のヒント

Don't use scriptlets; that's 1999 style. Learn JSTL and write your JSPs using that.

Your servlets should never, ever have embedded HTML in them. Just validate and bind parameters, pass them off to services for processing, and put the response objects in request or session scope for the JSP to display.

I agree with duffymo that you should learn on newer technology (if this is applicable, maybe your client cannot allow that...). Anyway, to get the value of the attribute you owuld do:

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

Also, I notice you have two different paths for your servlets in your HTML < form> tags:

MyWebArchive/MulitProcessorServlet

and

Week05WebArchive/MulitProcessorServlet

Is it expected?

You used Session not Request. you may need to get Session from request.

String strMasjidLocation = request.getSession().getAttribute("MasjidLocation");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top