Java servlet:request.getattribute总是重新返回null。 [英] Java servlets: request.getattribute always retun null.

查看:115
本文介绍了Java servlet:request.getattribute总是重新返回null。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类DepartmentBean中的addiFAbssent函数检查容器中是否已存在元素:



我尝试过:



Function addiFAbssent in class DepartmentBean check if element already exist in container:

What I have tried:

bool f;
public void addIfAbsent(UserBean userBean) {
            if (users.stream().anyMatch(x -> x.getUsername().equals(userBean.getUsername()))) {
                f = false;
            } else {
                f = true;
                users.add(userBean);
            }
        }
    }





在我的servlet中我试图发送成功注册的消息(如果f = false,他们没有同名的用户名):





In my servlet I am trying to send Message of succeseful registration(if f=false, their's no username with the same name):

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
                doAll(request, response);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
public static void doAll(HttpServletRequest request, HttpServletResponse response) throws Exception {
             DepartmentBean dp = new DepartmentBean();
             UserBean user = new UserBean();
             DepartmentBean departmentBean = read();
                String userName = request.getParameter("username");
                String password = request.getParameter("password");
                user.setPassowrd(password);
                user.setUsername(userName);
                departmentBean.addIfAbsent(user);
    if(dp.f = true)
    {
        String Message = "msg";
        /*Set attribute msg of Message*/
        request.setAttribute(Message, "msg");
        RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
         rd.forward(request, response);
         write(departmentBean);
    }
        }
        public static DepartmentBean read() throws JAXBException {
            JAXBContext context = JAXBContext.newInstance(DepartmentBean.class, UserBean.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            return (DepartmentBean) unmarshaller.unmarshal(new StreamSource(new File("1.xml")));
        }
        public static void write(DepartmentBean department) throws JAXBException {
            JAXBContext context = JAXBContext.newInstance(DepartmentBean.class, UserBean.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(department, new File("1.xml"));
        }
    }



在我的html中我添加了这个jsp代码:


In my html I added this jsp code:

<%>
    String s = "msg";
    if (s == request.getAttribute("Message"))
    {  
        out.println("<p>Successful</p>");
    }
    else 
    {
        out.println("<p>Error reg</p>");
    }
    <%>





当我检查xml文件时,记录是成功的,但是他们没有我的HTML中的成功段落。它总是返回null,错误注册段落。



When I check the xml file the record is succesful but their's no "Successful" paragraph in my html. It always retuns null, the "Error reg" paragraph.

推荐答案

        String Message = "msg";
        /*Set attribute msg of Message*/
        request.setAttribute(Message, "msg"); // so the attribute name is "msg"

...

    String s = "msg";
    if (s == request.getAttribute("Message")) // but the attribute name is not "Message"



您的属性名称不同。使用匹配的名称和更有意义的内容。


Your attribute names are different. Use names that match, and content that is more meaningful.


这篇关于Java servlet:request.getattribute总是重新返回null。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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