Using a jsp form to send a string to a servlet - using setAttribute in the jsp and getAttribute in the servlet
getattribute, java, jsp, servlets, setattribute
Solution
This should work: String value = (String) req.getSession(false).getAttribute("MasjidLocation")
Problem
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(); } } ```