Home >Java >javaTutorial >How to assign value to jsp page in Java

How to assign value to jsp page in Java

(*-*)浩
(*-*)浩Original
2019-05-29 11:47:124745browse

Use forwarding to assign values ​​to jsp pages. The specific operation is as follows:

How to assign value to jsp page in Java

This uses forwarding in the servlet to implement parameter transfer

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    request.setCharacterEncoding("UTF-8");
    request.setAttribute("username", "dhweicheng");
    request.setAttribute("password", "123456");
    request.getRequestDispatcher("/my.jsp").forward(request, response);
}

jsp page code:

<%@ 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>页面代码</title>
    </head>
    <body>
        <h5>通过request对象取值</h5>
        账号:<%=request.getAttribute("username") %>
        密码:<%=request.getAttribute("password") %>
        <P>=======================</P>
        <h5>通过EL表达式取值</h5>
        账号:${username}
        密码:${password}
    </body>
</html>

The above is the detailed content of How to assign value to jsp page in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn