packagecom.tgb.jstl;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.sun.org.apache.bcel.internal.generic.NEW;/*** 测试EL表达式
*@author巨亚红
* @date 2014-1-7 下午6:26:20
* @版本 V1.0 作者: 时间: 修改:*/
public class JstlElServlet extendsHttpServlet {/*** Constructor of the object.*/
publicJstlElServlet() {super();
}/*** Destruction of the servlet.
*/
public voiddestroy() {super.destroy(); //Just puts "destroy" string in log//Put your code here
}/*** The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
*@paramrequest the request send by the client to the server
*@paramresponse the response send by the server to the client
*@throwsServletException if an error occurred
*@throwsIOException if an error occurred*/
public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {//普通字符串
request.setAttribute("hello", "hello world");//结构
Group group=newGroup();
group.setName("提高班八期");
User user=newUser();
user.setName("juyahong");
user.setAge(25);
user.setGroup(group);
request.setAttribute("user", user);//map
Map map=newHashMap();
map.put("key1", "value1");
map.put("key2", "value2");
request.setAttribute("map", map);//字符串数组
String[] strArray=new String[]{"六期","七期","八期","九期","十期"};
request.setAttribute("str_array", strArray);//对象数组
User[] users=new User[5];for (int i = 0; i < users.length; i++) {
users[i]=newUser();
users[i].setName("juyahong("+i+")");
}
request.setAttribute("users", users);//list
List groupList=newArrayList();for (int i = 6; i < 12; i++) {
Group group2=newGroup();
group2.setName("提高班第"+i+"期");
groupList.add(group2);
}
request.setAttribute("groupList", groupList);//empty
request.setAttribute("tgb6", "");
request.setAttribute("tgb7", newArrayList());
request.setAttribute("tgb8", "提高班第八期");
request.setAttribute("tgb9", null);//request的分发器
request.getRequestDispatcher("/jstl_el.jsp").forward(request, response);
}/*** The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
*@paramrequest the request send by the client to the server
*@paramresponse the response send by the server to the client
*@throwsServletException if an error occurred
*@throwsIOException if an error occurred*/
public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println(""-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("");
out.println("
A Servlet");out.println("
");out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" ");
out.println("");
out.flush();
out.close();
}/*** Initialization of the servlet.
*
*@throwsServletException if an error occurs*/
public void init() throwsServletException {//Put your code here
}
}