java jstl_Java初学者必学的JSTL

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值