无法将数据从servlet发送到jsp

时间:2018-02-28 18:58:48

标签: java jsp servlets

我正在做教程,但它似乎是旧的或某些东西或我是盲目的。无论如何我不能发送java对象在JSP中显示。我正在使用请求属性。也许我做错了什么?

我的servlet

    package devcastzone.javaee.serwlety;


import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import devcast.javaee.Uzytkownik;

public class HelloWorld extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException
    {
        Uzytkownik user = new Uzytkownik();
        user.setId(1);
        user.setName("Janek");
        user.setNazwisko("Dzbanek");
        req.setAttribute("User", user);
        RequestDispatcher rd =getServletContext().getRequestDispatcher("/index.jsp");
        rd.forward(req,  res);
    }

}

我的jsp

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <p>helo</p> 
    ${User.name} 
    ${User.nazwisko}
</body>
</html>

和我的所有getter和setter的用户类

    package devcast.javaee;

import java.io.Serializable;

public class Uzytkownik implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 7585758287487776642L;
    private int id;
    private String name;  
    private String nazwisko;

    public int getId() {
        return this.id;
    }

    public void setId(int id){
        this.id = id;
    }

    public String getNazwisko() {
        return nazwisko;
    }

    public void setNazwisko(String nazwisko) {
        this.nazwisko = nazwisko;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

如果它很愚蠢我抱歉,但我找不到错误............................

0 个答案:

没有答案