Reputation: 569
Here is my HTML code in JSP
<select name="urlsel" id="selurl">
<option value="eng" name="eng"/>
<option value="mat" name="mat"/>
<option value="sci" name="sci"/>
</select>
<input type="submit" value="submit option" onsubmit="return validate()"/>
<% String opt=session.getAttribute("urlsel");
System.out.println("\n selected optiion is:+opt)
%>
The above JSP code is giving null value for opt. I tried with request.getParameter("urlsel");
in JSP still getting null.
I want the selected option value answer with session.getAttribute("urlsel");
How can I get it? I want this value in my servlet and in servlet using session.getAttribute("urlsel");
but getting null.
Please help me.
Upvotes: 3
Views: 83437
Reputation: 16376
In order to get attributes from a session or a request, before doing that you must set/add it somewhere in your code (i.e. first set attributes, then you can get them).
So the short answer: in your case, instead of using getAttribute(String name)
on session or request object, use request.getParameter(String name)
.
What you're trying to accomplish here is retrieving parameters passed using HTML form. In order to do that use getParameter(String name)
method, like:
String selectValue = request.getParameter("urlsel")
Traditionally values passed using HTTP form are retrieved in a servlet and then you can do whatever you need to do with them.
As I can see in your example, you are doing this PHP way. Although not the most common way in Java Web Application development, but you can retrieve parameters passed using HTML form in JSP from the param implicit object using Expression Language (you should avoid scriptlets in your JSP).
This is a simple example of a JSP page to showcase retrieving a parameter submitted using HTML form element in the same page (as in your example):
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Test Page</h1>
<form action="" method="post">
<p>Choose some course</p>
<select name="course">
<option value="English" name="eng">English</option>
<option value="Math" name="mat">Math</option>
<option value="Computer Science" name="sci">Computer Science</option>
</select>
<p><input type="submit" value="Pass data" /></p>
</form>
<hr />
<h2>Testing passed parameters</h2>
<p>Passed "course" parameter = <span style="color: #FF0000">${param.course}</span></p>
</body>
</html>
Note the usage of EL: ${param.course}
, where "param" is the name of one of the implicit objects, and "course" is the name of the select element, whose value was passed by submitting the HTML form.
Also check out the following answer for the additional information: Passing variables from JSP to servlet.
P.S.
You may find it useful to read some tutorials covering servlets and JSP. Here is a popular tutorial with nice explanations and easy to understand examples:
Beginning & Intermediate Servlet & JSP Tutorials
Upvotes: 5