index.jsp`
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action = "release.jsp" methon = post>
<table>
<tr>
<td>
<b>新闻发布</b>
</td>
</tr>
<tr>
<td>标题</td>
<td>
<input type = "text" name = "title" >
</td>
</tr>
<tr>
<td>内容</td>
<td>
<input type = "text" name = "content" >
</td>
</tr>
<tr>
<td>
<input type = "submit" value = "提交">
</td>
</tr>
</table>
</form>
</body>
</html>
release.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="newsRelease.encStr"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align = "center">
<%
String text = request.getParameter("title");
String content = request.getParameter("content");
%>
<table>
<td>
标题是:<%=text %>
内容是:<%=content %>
</td>
</table>
</div>
<jsp:useBean id="aNews" class="newsRelease.News"></jsp:useBean>
<jsp:setProperty property="*" name="aNews"/>
<h1>标题:
<%=encStr.getStr(aNews.getTitle()) %>
</h1>
<h4>
内容:
<%=encStr.getStr(aNews.getContent()) %>
</h4>
</body>
</html>
dataTime.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="refresh" content="1">
<title>Insert title here</title>
</head>
<body>
<%=new Date() %>
<jsp:useBean id="aDate" class="newsRelease.DateTime"></jsp:useBean>
<jsp:getProperty property="dateTime" name="aDate"/>
<jsp:getProperty property="week" name="aDate"/>
</body>
</html>
创建newselease包
encStr类用于解决乱码
package newsRelease;
import java.io.UnsupportedEncodingException;
public class encStr {
public static String getStr(String str){
String nStr="";
try {
nStr = new String(str.getBytes("ISO8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return nStr;
}
}
news类
package newsRelease;
public class News {
private String title;
private String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
datatime类 用于显示时间
package newsRelease;
import java.text.SimpleDateFormat;
import java.util.*;
public class DateTime {
private Calendar calendar= Calendar.getInstance();
private String week;
public String getDateTime(){
Date curDate=calendar.getTime();
SimpleDateFormat sdf=new SimpleDateFormat("yyy年MM月dd日 HH时mm分ss秒");
return sdf.format(curDate);
}
public String getWeek(){
String[] weeks={"周日","周一","周二","周三","周四","周五","周六"};
int intWeek=calendar.get(Calendar.DAY_OF_WEEK);
week= weeks[intWeek-1];
return week;
}
}