Jsp Servlets:从不同浏览器访问时显示空的上下文对象
我最近了解到,上下文对象不会因不同的用户或servlet而改变Jsp Servlets:从不同浏览器访问时显示空的上下文对象,jsp,servlets,jakarta-ee,Jsp,Servlets,Jakarta Ee,我最近了解到,上下文对象不会因不同的用户或servlet而改变 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ response.setContentType("text/html"); PrintWriter out=response.getWriter(); HttpSession s
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
HttpSession session=request.getSession();
String username=request.getParameter("username");
ServletContext context=request.getServletContext();
try{
if(username!=null && !username.isEmpty())
session.setAttribute("savedUserName", username);
context.setAttribute("name", username);
}
catch(NullPointerException e){
}
out.println("Request parameter has username: "+username);
out.println("Session parameter has username: "+(String)session.getAttribute("savedUserName"));
out.println("Context parameter has username: "+(String)context.getAttribute("name"));
}
当我使用setAttribute设置上下文对象并运行servlet时,它将打印正确的值。但是如果我尝试使用不同的浏览器运行servlet,它将显示null。据我所知,上下文对象值应该适用于所有servlet。在上面的程序中,我从URL获取参数。我的代码有什么问题?as我对代码的理解是,请使用带if条件的括号(
{}
)。当unsername不为null时,它将允许设置name属性的值
在代码中,context.setAttribute(“名称”,用户名)当您从另一个web浏览器点击servelt时,代码不在“如果条件显示”设置为空值的范围内
try{
if(username!=null && !username.isEmpty()){
session.setAttribute("savedUserName", username);
context.setAttribute("name", username);
}
}
catch(NullPointerException e){
}
根据我在代码中的理解,请使用带if条件的
{}括号。当unsername不为null时,它将允许设置name属性的值
在代码中,context.setAttribute(“名称”,用户名)当您从另一个web浏览器点击servelt时,代码不在“如果条件显示”设置为空值的范围内
try{
if(username!=null && !username.isEmpty()){
session.setAttribute("savedUserName", username);
context.setAttribute("name", username);
}
}
catch(NullPointerException e){
}
您可能在两个请求之间重新启动了服务器或重新部署了应用程序。您可能在两个请求之间重新启动了服务器或重新部署了应用程序。