javabean中的代码package bean;public class person { private int id;
private String name;
private int age;
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
}servlet中的代码package Servlet;
import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import bean.person;
public class HelloPersonServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
person ps=new person();
ps.setId(100);
ps.setName("小王");
ps.setAge(22);
request.getSession().setAttribute("Person",ps);
response.sendRedirect("helloperson.jsp");
}}jsp中的代码<%@ page language="java" contentType="text/html; charset=gb2312" import="bean.person"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'helloperson.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<jsp:useBean id="ps" class="bean.person" scope="session"/>
<jsp:getProperty property="age" name="ps"/>
</body>
</html>web.xml的代码是<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>HelloPersonServlet</servlet-name>
<servlet-class>Servlet.HelloPersonServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>HelloPersonServlet</servlet-name>
<url-pattern>/reg</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>使用的是doGet方法,在浏览器中使用的url是http://localhost:8080/JavabeanTest/reg,只能显示javabean中属性的初始值,不知道为什么使用servlet修改不了,请各位大虾指教
private String name;
private int age;
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return this.age;
}
public void setAge(int age) {
this.age = age;
}
}servlet中的代码package Servlet;
import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import bean.person;
public class HelloPersonServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
person ps=new person();
ps.setId(100);
ps.setName("小王");
ps.setAge(22);
request.getSession().setAttribute("Person",ps);
response.sendRedirect("helloperson.jsp");
}}jsp中的代码<%@ page language="java" contentType="text/html; charset=gb2312" import="bean.person"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'helloperson.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head>
<body>
<jsp:useBean id="ps" class="bean.person" scope="session"/>
<jsp:getProperty property="age" name="ps"/>
</body>
</html>web.xml的代码是<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>HelloPersonServlet</servlet-name>
<servlet-class>Servlet.HelloPersonServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>HelloPersonServlet</servlet-name>
<url-pattern>/reg</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>使用的是doGet方法,在浏览器中使用的url是http://localhost:8080/JavabeanTest/reg,只能显示javabean中属性的初始值,不知道为什么使用servlet修改不了,请各位大虾指教
楼主你注意下,你在session中存放ps的key值为Person,但是你在页面取的时候是怎样取的。。
最简答就是使用el表达式${Person.age}可以直接取到了(前提是你new的项目要有el的jar包)。。