0

嗨,我可以知道如何将返回值从普通的 java 类传递给 servlet,然后使用 jsp 显示它吗?我试过但只得到空值。

结果.java

package langID; 

public class Result {

public String Show()
{

    return "result";

}
}

Formhandler.java (servlet)

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

RequestDispatcher rd = request.getRequestDispatcher("comment.jsp");    
Result demo = new Result();
String s = demo.Show();
request.setAttribute("s", s);  
rd.forward(request, response);

}

评论.jsp

<%String name = (String)request.getAttribute("s"); %>
                    <%= s%>
4

2 回答 2

0

这是您必须编写的jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<jsp:useBean id="s" scope="request" class="java.lang.String"></jsp:useBean>
<%=s %>
</body>
</html>
于 2013-04-19T04:47:01.197 回答
0

这里需要使用 <%= name%> not<%= s%>

因为您的值现在可用 in namenot ins

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/comment.jsp?
 valpass=val");


 Result demo = new Result();
         String s = demo.Show();
          request.setAttribute("s", s);  
        dispatcher.forward(request, response); 
于 2013-04-19T03:50:38.173 回答