jsp 中的getParameter和getAttribute

从页面传到后台action中的参数可以用三种方式接收:

1.声明一个变量,然后设置其getter和set方法

private String username;

 public String getUsername() {
  return username;
 }
 public void setUsername(String username) {
  this.username = username;
 }

2.通过getParameter

request.getParameter("username");

3,通过getAttribute

request.getAttribute("username");

在jsp页面中的通过request.setAttribute("pwd","123456");设置的参数,在后台用上述三种方法都取不到值。

在后台action中通过request.setAttribute("pwd","123456");设置的参数,在jsp页面中可以用request.getAttribute("pwd");获得。

在jsp页面中:可以通过${person }获得action中通过ServletActionContext.getRequest().setAttribute("person", person);设置的值。

通过?方式传给action的参数比如:http://localhost:8080/hello/act1.action?fileName=jquery.chm,在页面中通过${param.fileName}获得其值;如果action中有fileName的setter和getter方法,则还可以通过${fileName}的方式获得。

原文地址:https://www.cnblogs.com/passer1991/p/2967058.html