将值从Servlet写入JSP

时间:2021-06-01 11:53:15

heloo i want to write something from my servlet to jsp file. i did it using requestdispatcher but result didn't appear

heloo我想从我的servlet写一些东西到jsp文件。我使用requestdispatcher做了但结果没有出现

here are codes:

这是代码:

UserInfo.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = null;
        Connection conn = null;
        DBUtils dbUtils = null;
        try{
            out = response.getWriter();     
            String username = null;
            String password = null;

            //setting cookies for session
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                if(cookie.getName().equals("username"))
                    username = cookie.getValue();
            }
            }

            request.setAttribute("username", username);
            request.getRequestDispatcher("userinfo.jsp").forward(request, response);

userinfo.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Hello ${username}</h1>
    </body>
</html>

3 个解决方案

#1


0  

Try using request.getAttribute("username") in your jsp and include it <% %> tag.

尝试在jsp中使用request.getAttribute(“username”)并将其包含在<%%>标记中。

#2


0  

Try this..

ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/userinfo.jsp");
rd.forward(request,response);

Note the difference when i am using /userinfo.jsp.. using it to provide the absolute path for jsp location. if there was no preceding "/" it will be considered for relative path and appends userinfo.jsp to existing request relative path.

注意我使用/userinfo.jsp时的区别..使用它来提供jsp位置的绝对路径。如果没有前面的“/”,它将被考虑用于相对路径并将userinfo.jsp附加到现有的请求相对路径。

OR if the above does not work rewrite this line to use absoulute path

或者,如果以上不起作用,则重写此行以使用absoulute路径

request.getRequestDispatcher("/userinfo.jsp").forward(request, response);

#3


0  

use in your jsp no need to iterate

在你的jsp中使用无需迭代

${cookie.username.value}

#1


0  

Try using request.getAttribute("username") in your jsp and include it <% %> tag.

尝试在jsp中使用request.getAttribute(“username”)并将其包含在<%%>标记中。

#2


0  

Try this..

ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/userinfo.jsp");
rd.forward(request,response);

Note the difference when i am using /userinfo.jsp.. using it to provide the absolute path for jsp location. if there was no preceding "/" it will be considered for relative path and appends userinfo.jsp to existing request relative path.

注意我使用/userinfo.jsp时的区别..使用它来提供jsp位置的绝对路径。如果没有前面的“/”,它将被考虑用于相对路径并将userinfo.jsp附加到现有的请求相对路径。

OR if the above does not work rewrite this line to use absoulute path

或者,如果以上不起作用,则重写此行以使用absoulute路径

request.getRequestDispatcher("/userinfo.jsp").forward(request, response);

#3


0  

use in your jsp no need to iterate

在你的jsp中使用无需迭代

${cookie.username.value}