问题一:解释代码:
String str = request.getParameter("name");
String value = new String(str.getBytes("ISO-8859-1"),"GBK");
因为tomcat容器默认采用了..iso-8859-1的编码方式,应该是将str用iso8859-1的编码方式取出,然后用gbk的方式编码输出。
JavaBean类:DateBean
1 package com.liu.bean; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Calendar; 5 import java.util.Date; 6 7 public class DateBean { 8 private String date; 9 private String week; 10 private Calendar calendar = Calendar.getInstance(); 11 12 public String getDate() 13 { 14 //获取当前时间 15 Date current = Calendar.getInstance().getTime(); 16 SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH小时mm分ss秒"); 17 date = format.format(current); 18 return date; 19 } 20 public String getWeek() 21 { 22 String[] weeks = {"星期日","星期一","星期二","星期三","星期四","星期五","星期六"}; 23 int index = calendar.get(Calendar.DAY_OF_WEEK); 24 week = weeks[index-1]; 25 return week; 26 } 27 }
DateBean.jsp页面
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'DateBean.jsp' starting page</title> 13 14 15 16 </head> 17 18 <body> 19 <jsp:useBean id="date" class="com.liu.bean.DateBean"></jsp:useBean> 20 <div align="center"> 21 <jsp:getProperty property="date" name="date"/><br/> 22 <jsp:getProperty property="week" name="date"/> 23 </div> 24 </body> 25 </html>