3

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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
user2515189
  • 539
  • 3
  • 9
  • 19
  • 1
    Are you ever setting the session attribute? Since you tried both a session attribute and request parameter, which have nothing to do with each other, I'm guessing that you are trying to do something client-side? JSP doesn't make sense for that. – Brandon Jun 24 '13 at 12:17
  • Not related to your question.. You are using Scriplets which is no more used these days. Instead replace them with [JSTL](http://www.oracle.com/technetwork/java/index-jsp-135995.html) – Vikas V Jun 24 '13 at 12:58

1 Answers1

5

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).


Simple example

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

Community
  • 1
  • 1
informatik01
  • 16,038
  • 10
  • 74
  • 104
  • I need the selected value not only for one time in servlet.My jsp is getting refreshed for every 10 seconds.In that case it is unable to get the already selected option.So I am trying with sessions.But unable to get the values using session.getAttribute() – user2515189 Jun 25 '13 at 04:40
  • @user2515189 As I already wrote, you **first** need to put the value in *session*, **only then** you can get it from there. Like `String selectValue = request.getParameter("urlsel"); request.getSession().setAttribute("urlsel", selectValue)`, something like that. Plus you never wrote anything about refreshing your page and preserving the selected value. **Please try to be precise about the details when writing your question**. – informatik01 Jun 25 '13 at 13:10
  • @user2515189 I have posted [an answer to your question](http://stackoverflow.com/a/17302734/814702), where you described the refresh issue. – informatik01 Jun 25 '13 at 20:12