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

I am experimenting with submitting data from a jsp form and calling a servlet and showing that data in a servlet.

I would like to use setAttribute and getAttribute.

In this jsp file, I use 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 that I would like to use getAttribute, but I do not know how to use GetAttribute. Can you show me what extra code I need to add to the servlet so that I can capture the value from 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();
}
}
+3
source share
4 answers

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

+4
source

; 1999 . JSTL JSP, .

HTML-. , JSP.

+3

I agree with duffymo that you should learn newer technology (if applicable, your client may not allow this). In any case, to get the value of the attribute you are executing:

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

In addition, I noticed that you have two different paths for your servlets in your HTML <form>:

MyWebArchive/MulitProcessorServlet

and

Week05WebArchive/MulitProcessorServlet

Is it expected?

+2
source

You used Session not Request. You may need to get a session from the request.

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

All Articles