java 如何在Session的帮助下通过Bean将参数从Servlet传递到JSP页面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5798057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 12:48:20  来源:igfitidea点击:

How to pass parameters from Servlet via Bean to JSP page with the help of Session?

javajspsessionservletsjavabeans

提问by a k

As mentioned below I have made changes to the code which looks like following example, but it doesn't show firstname and lastname in JSP:

如下所述,我对代码进行了更改,如下例所示,但它没有在 JSP 中显示名字和姓氏:

Servlet code:

服务端代码:

//....
HttpSession session = request.getSession();
Person person = (Person) session.getAttribute("person");
if (person == null) {
    person = new Person();                      
}
person.setNewId(newId);
person.setFirstName(firstName);
person.setLastName(lastName);
session.setAttribute("person", person);

RequestDispatcher rd = request.getRequestDispatcher("jsp Address");
rd.forward(request, response);

Person Bean code:

人豆代码:

private int newId;
private String firstName;
private String lastName;

// Default Constructor
public Person() {}

public Person(int newId, String firstName, String lastName) {
    setNewId(newId);
    setFirstName(firstName);
    setLastName(lastName);
}

//Getter and Setter Methods
public int getNewId() {return newId;}
public void setNewId(int newID) {this.newId = newID;}
public String getFirstName() {return firstName;}
public void setFirstName(String FirstName) {this.firstName = FirstName;}
public String getLastName() {return lastName;}
public void setLastName(String LastName) {this.lastName = LastName;}

And in JSP code:

在 JSP 代码中:

<jsp:useBean id="person" type="app.models.Person" scope="session">
    <jsp:getProperty name="person" property="firstName" />
    <jsp:getProperty name="person" property="lastName" />
</jsp:useBean>

Output of this JSP page: none

此 JSP 页面的输出:无

Expected output: firstName lastName

预期输出:名字姓氏

Questions:

问题:

1. How can i pass parameters from Servlets to JSP via Bean with help of Session? 
2. Is there a better way to do this code? I am using MVC architecture.

采纳答案by BalusC

Get rid of the <jsp:useBean>. When using the typeattribute instead of class, it won't check if there's already an instance in the scope, it will plain override the Personinstance which you created in the servlet with a new, blank, default constructed instance.

摆脱<jsp:useBean>. 当使用type属性而不是 时class,它不会检查作用域中是否已经有一个实例,它会Person用一个新的、空白的、默认构造的实例简单地覆盖您在 servlet 中创建的实例。

When using "MVC architecture", the <jsp:useBean>tag is useless. Remove it and just use usual ELto access it:

当使用“MVC 架构”时,<jsp:useBean>标签是无用的。删除它并使用通常的EL来访问它:

${person.firstName} ${person.lastName}

Or better, to prevent XSS attacks, use JSTL<c:out>.

或者更好的是,为了防止XSS 攻击,请使用JSTL<c:out>

<c:out value="${person.firstName} ${person.lastName}" />