嗨我可以知道如何将返回值从普通的java类传递给servlet然后使用jsp显示它吗?我试过但只有空值。
Result.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);
}
comment.jsp
<%String name = (String)request.getAttribute("s"); %>
<%= s%>
答案 0 :(得分:0)
您需要使用<%= name%>
而不是<%= s%>
因为现在name
中的值不在s
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/comment.jsp?
valpass=val");
Result demo = new Result();
String s = demo.Show();
request.setAttribute("s", s);
dispatcher.forward(request, response);
答案 1 :(得分: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>