将Java值发送到网页
我的Java代码中有两个变量。 我的代码如下所示:将Java值发送到网页,java,string,variables,int,Java,String,Variables,Int,我的Java代码中有两个变量。 我的代码如下所示: String user = "Example"; int score = "100" <table> <tr> <td>user</td> <td>score</td> </tr> </table> 现在我想要的是,把这些发送到一个网页上。 看起来像这样: String user = "Example"; in
String user = "Example";
int score = "100"
<table>
<tr>
<td>user</td>
<td>score</td>
</tr>
</table>
现在我想要的是,把这些发送到一个网页上。
看起来像这样:
String user = "Example";
int score = "100"
<table>
<tr>
<td>user</td>
<td>score</td>
</tr>
</table>
用户
分数
我该怎么办呢
请原谅我的英语不好,如果您能提供帮助,那就太好了。假设您使用的是servlet,您可以使用
request
参数来传递值
发送数据
request.setAttribute("user ","Example");
request.setAttribute("score ", 100);
接收数据
request.getAttribute("user");
request.getAttribute("score");
在Servlet类中:
public class TestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user = "Example";
int score = 100;
//Set your attribute in your request scope
request.setAttribute("user", user);
request.setAttribute("score", score);
//Set your attribute in your Session scope
//request.getSession().setAttribute("user", user);
//request.getSession().setAttribute("score", score);
//Other Stuffs
}
}
在jsp页面中,您可以通过两种方式获取值:1-Scriplet2-JSTL
<table>
<tr>
<td>user</td>
<td>score</td>
</tr>
<tr>
<td><%=request.getAttribute("user")%></td>
<td><%=request.getAttribute("score")%></td>
</tr>
<!-- IF YOU HAVE JSTL NAMESPACE ENABLED YOU CAN FETCH IT LIKE THIS -->
<%--
<tr>
<td>${user}</td>
<td>${score}</td>
</tr>
--%>
</table>
用户
分数
要在JSP中启用JSTL,您应该先将JSTL库添加到项目中,然后在JSP页面顶部添加此命名空间:
<%@taglib uri=”http://java.sun.com/jsp/jstl/core“prefix=“c”%>
祝你好运