大家可能看不清楚,这是我自己写的。等自己有时间来解决。
1、Servlet的类的写法
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletPerson extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String path="JGPerson.jsp";
String uname=req.getParameter("name");
String uage=req.getParameter("age");
Person p=new Person();
p.setAge(Integer.parseInt(uage));
p.setName(uname);
req.setAttribute("person", p);
req.getRequestDispatcher(path).forward(req, resp);
}
}
2、接受值的jsp页面的写法
<%@ page language="java" contentType="text/html"
pageEncoding="utf-8"%>
<html>
<head>
<title>this first jsp and Servlet</title>
</head>
<body>
<%=request.getAttribute("person").getClass()%><br>
姓名:${person.name}<br>
年龄:${person.age}<br>
</body>
</html>
3、问题是:我不想通过$表达式,得到Person类里面的值,也就是页面传过来的。
还有别的方法吗?我既然可以得到<%=request.getAttribute("person").getClass()%><br>
我能不能通过get方法,拿到值吗?