JSP中的session.getAttribute()给出null值 [英] session.getAttribute() in JSP is giving null value

查看:179
本文介绍了JSP中的session.getAttribute()给出null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在JSP中的HTML代码

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

上面的JSP代码为 opt 提供了 null 值。我尝试使用 request.getParameter(urlsel); JSP中的仍然 null

The above JSP code is giving null value for opt. I tried with request.getParameter("urlsel"); in JSP still getting null.

我希望选中的选项值回答 session .getAttribute(urlsel);

I want the selected option value answer with session.getAttribute("urlsel");

我怎样才能得到它?我想在我的servlet和servlet中使用 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.

请帮助我。

推荐答案

为了从会话请求获取属性,在此之前,您必须在代码中的某处设置/添加它(即第一个设置属性,然后您可以获取它们)。

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

所以简短回答:在你的情况下,而不是在 getAttribute(String name) m> session 或 request 对象,使用 request.getParameter(String name)

So the short answer: in your case, instead of using getAttribute(String name) on session or request object, use request.getParameter(String name).

您在此处尝试完成的工作是检索使用HTML表单传递的参数。为了做到这一点,使用 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)

传统上使用HTTP表单传递的值在servlet中检索然后你可以做任何你需要做的事情。

Traditionally values passed using HTTP form are retrieved in a servlet and then you can do whatever you need to do with them.

正如我在你的例子中所看到的,你正在做这个 PHP 方式。虽然不是Java Web应用程序开发中最常用的方法,但您可以从 param 隐式对象使用表达式语言(您应该避免JSP中的scriptlet )。

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

这是一个JSP页面的简单示例,用于展示检索使用HTML 表单提交的参数同一页面中的元素(如示例所示):

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>



注意使用 EL $ {param.course} ,其中 param 是其中一个隐式对象 course 选择的名称元素,其值通过提交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.

另请查看以下答案以获取更多信息:从JSP到servlet的变量传递

Also check out the following answer for the additional information: Passing variables from JSP to servlet.

PS

您可能会发现阅读一些有关servlet和JSP的教程很有用。这是一个流行的教程,有很好的解释和易于理解的例子:

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:

开始&中级Servlet& JSP教程

这篇关于JSP中的session.getAttribute()给出null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆