heloo我想寫一些從我的servlet到jsp文件。我做到了使用的RequestDispatcher,但結果並沒有出現從Servlet寫入值到JSP
這裏是代碼:
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>
請你能否解釋一下'結果沒有appear' ?你是否重定向到你請求的頁面(ie)'userinfo.jsp' –
$ {username} theres什麼都沒有 – user2513301
我使用ajax重定向喲userinfo.jsp – user2513301