会话属性在首次加载时为null [英] Session attribute is null at first load

查看:53
本文介绍了会话属性在首次加载时为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下servlet:

@WebServlet(name = "Placeholder",urlPatterns = {"/foo"})
public class Placeholder extends HttpServlet {
    public static int numbers=5;
    HttpSession session;
    public void doGet (HttpServletRequest _req, HttpServletResponse _res) throws ServletException, IOException {
        /* Refresh session attributes */
        session = _req.getSession();
        session.setAttribute("wee","ok");
        }
}

具有以下JSP:

<%@page language="java" contentType="text/html"%>
<%@page import="java.util.*, java.io.*"%>
<%@page import="main.java.Placeholder.*" %>
<html>
<body>
<b><% out.println("wee, printing from java");%></b>
<% out.println("<br/>Your IP address is " + request.getRemoteAddr());
String value = (String) session.getAttribute("wee");
out.println(value);%>
</body>
</html>

当我第一次加载页面时,由于属性wee被解析为null,所以我肯定缺少该点.如果转到/foo,我将清空页面,然后返回并重新加载servlet的根页,则wee实际上会获得其值.

I'm surely missing the point somewhere as attribute wee is resolved as null, first time I load the page. If I go to /foo I get empty an page, and after I get back and reload the root page of servlet, wee actually gets its value.

我在这里的目标是简单地将servlet中的变量打印到视图中,而不需要路由.不确定这里是否需要urlPatterns,但是如果没有这个小技巧,它目前无法正常工作.

My goal here is to simply print variables from the servlet into the view, no routing needed. Not sure that urlPatterns are needed here, but it does not work for now without this little hack.

UPD.好的,所以我弄清楚了无论我采用哪种路线,我都需要在浏览器中添加一些字符,返回并重新加载页面. 因此,根是0.0.0.0:8080/webapp

UPD. Ok, so I've figured out that whatever route I put in, I need to add some characters in browser, get back and reload the page. So, the root is 0.0.0.0:8080/webapp

我需要访问,例如说0.0.0.0:8080/webapp/qwerty,回到/webapp并刷新页面.

I need to access,say 0.0.0.0:8080/webapp/qwerty , get back to /webapp and refresh the page.

如何仅通过转到/webapp来实例化会话? 为什么我没有404或500来访问一些不存在的随机路由/webapp/randomstuff?

How do I get session instantiated by just going to /webapp? Why don't I have 404 or 500 on accessing some random unexisting route /webapp/randomstuff?

推荐答案

首先在web.xml中将servlet配置为欢迎文件.如果web.xml不存在,则可以在WEB-INF文件夹中手动创建它,并在其下面的内容下面放置

First configure servlet as welcome file in web.xml. If web.xml not present than create it manually inside WEB-INF folder and put below content inside it.

<welcome-file-list>
        <welcome-file>foo</welcome-file>
</welcome-file-list>

比您向jsp的servlet调度请求中的条件让我们说,您的jsp名称为index.jsp,比您的servlet代码看起来像这样:

than in your servlet dispatch request to your jsp lets say your jsp name is index.jsp than your servlet code would be look like:

@WebServlet(name = "Placeholder",urlPatterns = {"/foo"})
public class Placeholder extends HttpServlet {
    public static int numbers=5;
    public void doGet (HttpServletRequest _req, HttpServletResponse _res) throws ServletException, IOException {
        HttpSession session = _req.getSession();
        session.setAttribute("wee","ok");
        _res.sendRedirect("index.jsp");
        }
}

现在运行您的servlet,您将看到输出. 希望这能解决您的问题!!!

Now run your servlet you will see output. Hope this solve your problem!!!

这篇关于会话属性在首次加载时为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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