WebWork标签非常出色,在有些应用中,可能不想使用其环境,但希望能够使用其标签思路。JSP模板的使用面更广,随简化WebWork方式,提供思路供大家参考。
1、开发标签基础类:
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.Writer;
import java.util.Iterator;
import java.util.LinkedList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public abstract class AbstractTag extends BodyTagSupport {
protected String templateName ;
private final static String templatePath = "/WEB-INF/tags/";
private static final long serialVersionUID = -1201668454354226175L;
public String getTemplateName() {
return templateName;
}
public void setTemplateName(String templateName) {
this.templateName = templateName;
}
protected String getBody() {
if (bodyContent == null) {
return "";
} else {
return bodyContent.getString().trim();
}
}
protected abstract void prepareData ();
public int doEndTag() throws JspException {
try {
prepareData ();
include(templatePath + this.getTemplateName(), pageContext.getOut(),
pageContext.getRequest(),
(HttpServletResponse)
pageContext.getResponse());
} catch (Exception e) {
// e.printStackTrace();
throw new JspException(e);
}
return EVAL_BODY_INCLUDE;
}
public int doStartTag() throws JspException {
try {
pageContext.getOut().write(getBody());
} catch (IOException e) {
throw new RuntimeException("IOError: " + e.getMessage(), e);
}
return EVAL_PAGE;
}
public static void include(String aResult, Writer writer,ServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String resourcePath = aResult;
RequestDispatcher rd = request.getRequestDispatcher(resourcePath);
if (rd == null) {
throw new ServletException("Not a valid resource path:"
+ resourcePath);
}
// Include the resource
PageResponse pageResponse = new PageResponse(response);
// Include the resource
rd.include((HttpServletRequest) request, pageResponse);
// write the response back to the JspWriter, using the correct encoding.
String encoding = "GB2312";
if (encoding != null) {
// use the encoding specified in the property file
pageResponse.getContent().writeTo(writer, encoding);
} else {
// use the platform specific encoding
pageResponse.getContent().writeTo(writer, null);
}
}
static final class PageResponse extends HttpServletResponseWrapper {
protected PrintWriter pagePrintWriter;
protected ServletOutputStream outputStream;
private PageOutputStream pageOutputStream = null;
/**
* Create PageResponse wrapped around an existing HttpServletResponse.
*/
public PageResponse(HttpServletResponse response) {
super(response);
}
/**
* Return the content buffered inside the {@link PageOutputStream}.
*
* @return
* @throws IOException
*/
public FastByteArrayOutputStream getContent() throws IOException {
// if we are using a writer, we need to flush the
// data to the underlying outputstream.
// most containers do this - but it seems Jetty 4.0.5 doesn't
if (pagePrintWriter != null) {
pagePrintWriter.flush();
}
return ((PageOutputStream) getOutputStream()).getBuffer();
}
/**
* Return instance of {@link PageOutputStream} allowing all data written
* to stream to be stored in temporary buffer.
*/
public ServletOutputStream getOutputStream() throws IOException {
if (pageOutputStream == null) {
pageOutputStream = new PageOutputStream();
}
return pageOutputStream;
}
/**
* Return PrintWriter wrapper around PageOutputStream.
*/
public PrintWriter getWriter() throws IOException {
if (pagePrintWriter == null) {
pagePrintWriter = new PrintWriter(new
OutputStreamWriter(
getOutputStream(), getCharacterEncoding()));
}
return pagePrintWriter;
}
}
static final class PageOutputStream extends ServletOutputStream {
private FastByteArrayOutputStream buffer;
public PageOutputStream() {
buffer = new FastByteArrayOutputStream();
}
/**
* Return all data that has been written to this OutputStream.
*/
public FastByteArrayOutputStream getBuffer() throws IOException {
flush();
return buffer;
}
public void close() throws IOException {
buffer.close();
}
public void flush() throws IOException {
buffer.flush();
}
public void write(byte[] b, int o, int l) throws IOException {
buffer.write(b, o, l);
}
public void write(int i) throws IOException {
buffer.write(i);
}
public void write(byte[] b) throws IOException {
buffer.write(b);
}
}
static public class FastByteArrayOutputStream extends OutputStream {
// Static --------------------------------------------------------
private static final int DEFAULT_BLOCK_SIZE = 8192;
private LinkedList buffers;
// Attributes ----------------------------------------------------
// internal buffer
private byte[] buffer;
// is the stream closed?
private boolean closed;
private int blockSize;
private int index;
private int size;
// Constructors --------------------------------------------------
public FastByteArrayOutputStream() {
this(DEFAULT_BLOCK_SIZE);
}
public FastByteArrayOutputStream(int aSize) {
blockSize = aSize;
buffer = new byte[blockSize];
}
public int getSize() {
return size + index;
}
public void close() {
closed = true;
}
public byte[] toByteArray() {
byte[] data = new byte[getSize()];
// Check if we have a list of buffers
int pos = 0;
if (buffers != null) {
Iterator iter = buffers.iterator();
while (iter.hasNext()) {
byte[] bytes = (byte[]) iter.next();
System.arraycopy(bytes, 0, data, pos, blockSize);
pos += blockSize;
}
}
// write the internal buffer directly
System.arraycopy(buffer, 0, data, pos, index);
return data;
}
public String toString() {
return new String(toByteArray());
}
// OutputStream overrides ----------------------------------------
public void write(int datum) throws IOException {
if (closed) {
throw new IOException("Stream closed");
} else {
if (index == blockSize) {
addBuffer();
}
// store the byte
buffer[index++] = (byte) datum;
}
}
public void write(byte[] data, int offset, int length) throws IOException {
if (data == null) {
throw new NullPointerException();
} else if
((offset < 0) || ((offset + length) > data.length) || (length
< 0)) {
throw new IndexOutOfBoundsException();
} else if (closed) {
throw new IOException("Stream closed");
} else {
if ((index + length) > blockSize) {
int copyLength;
do {
if (index == blockSize) {
addBuffer();
}
copyLength = blockSize - index;
if (length < copyLength) {
copyLength = length;
}
System.arraycopy(data, offset, buffer, index, copyLength);
offset += copyLength;
index += copyLength;
length -= copyLength;
} while (length > 0);
} else {
// Copy in the subarray
System.arraycopy(data, offset, buffer, index, length);
index += length;
}
}
}
// Public
public void writeTo(OutputStream out) throws IOException {
// Check if we have a list of buffers
if (buffers != null) {
Iterator iter = buffers.iterator();
while (iter.hasNext()) {
byte[] bytes = (byte[]) iter.next();
out.write(bytes, 0, blockSize);
}
}
// write the internal buffer directly
out.write(buffer, 0, index);
}
public void writeTo(RandomAccessFile out) throws IOException {
// Check if we have a list of buffers
if (buffers != null) {
Iterator iter = buffers.iterator();
while (iter.hasNext()) {
byte[] bytes = (byte[]) iter.next();
out.write(bytes, 0, blockSize);
}
}
// write the internal buffer directly
out.write(buffer, 0, index);
}
public void writeTo(Writer out, String encoding) throws IOException {
// Check if we have a list of buffers
if (buffers != null) {
Iterator iter = buffers.iterator();
while (iter.hasNext()) {
byte[] bytes = (byte[]) iter.next();
if (encoding != null) {
out.write(new String(bytes, encoding));
} else {
out.write(new String(bytes));
}
}
}
// write the internal buffer directly
if (encoding != null) {
out.write(new String(buffer, 0, index, encoding));
} else {
out.write(new String(buffer, 0, index));
}
}
/**
* Create a new buffer and store the
* current one in linked list
*/
protected void addBuffer() {
if (buffers == null) {
buffers = new LinkedList();
}
buffers.addLast(buffer);
buffer = new byte[blockSize];
size += index;
index = 0;
}
}
}
2、定义一个具体的标签类
public class ListTag extends RiseAbstractTag {
private static final long serialVersionUID = 3385568988234498913L;
protected String templateName = "list.jsp";
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
protected void prepareData() {
this.setTemplateName(this.templateName);
pageContext.getRequest().setAttribute("id", this.id);
}
}
3、定义TLD文件
参考TLD文档
4、定义list.jsp模板
<%@ page contentType="text/html; charset=GBK" %>
<%
String id = (String)request.getAttribute("id");
%>
<table width="90%" border="0" cellpadding="0" cellspacing="2">
<tr>
<td>Id</td>
<td align="right"><%= id %></td>
</tr>
</table>
5、使用默认模板
<WWTag:list id="Hello World!"/>
6、使用自定义模板
a: 定义模板
<%@ page contentType="text/html; charset=GBK" %>
<%
String id = (String)request.getAttribute("id");
out.println("Id is : " + id);
%>
b: use it , 模板名:testList.jsp,放在/WEB-INF/tags目录下
<WWTag:list id="Hello World!" templateName="testList.jsp"/>
团队在外地封闭开发,没有带交换机功能能的HUB来连接到ADSL。只好共享ADSL方式。
1、windows下共享非常简单,把ADSL共享即可,但不要把每一个连接都拨号给选上,否则无法使用。此时局域网内IP地址在192.168.0.1--192.168.0.2XX之间。问题是:容易坏,不稳定。随改用Linux。
2、Linux环境下使用。Red Hat Linux ES3版本。(文档来自网络收集,共享大家使用)
http://www.chinalinuxpub.com/read.php?wid=558
1、 网卡配置。
我这里用的网卡是RTL8029和3com905。在系统中,RTL8029标记为eth0,3com905标记为eth1。RTL8029和3com905的IP地址分别是192.168.0.1、192.168.1.1(其他的地址也可),掩码均为255.255.255.0。
eth0用于连接网通,eth1用于连接内网,局域网网段为192.168.0.0。
注意:此处两块网卡均不能设网关。
2、 PPPoE软件的升级与安装
1) 在 http://www.roaringpenguin.com/pppoe/#download
下载
2) 安装rp-pppoe。以root身份执行
rpm –Uvh
rp-pppoe-3.5-1.i386.rpm
3、 修改/etc/ sysctl.conf
将其中的
net.ipv4.ip_forward
= 0
改为
net.ipv4.ip_forward
= 1
4、 去除ipchains模块,只选择iptables方法如下:
1)setup
2)选择system service
3)去除ipchains
4)选中iptables
5)重启机器
5、 PPPoE客户端配置
在rp-pppoe-3.5-1.i386.rpm安装完毕后,接下来就可进行PPPoE客户端配置了。过程如下。
#/usr/sbin/adsl-setup
>>>
Enter your PPPoE user name: ——此处输入拨号帐号的用户名
>>>
Enter the Ethernet interface connected to the ADSL modem For Solaris, this is
likely to be something like /dev/hme0. For Linux, it will be ethn, where 'n'
is a number. (default eth0): ——输eth0
>>>
Enter the demand value (default no): ——输no
>>>
Enter the DNS information here: ——输210.83.130.18
>>>
Please enter your PPPoE password: ——输网通用户口令
>>>
Choose a type of firewall (0-2): ——输0
>>>
Accept these settings and adjust configuration files (y/n)? ——输y
6、 启动拨号连接
/usr/sbin/adsl-start
成功连接后,屏幕显示Connected。
此时这台linux已可以上网浏览了。
7、 IP伪装
为了使局域网中的其他机器能通过Linux服务器共享上网,至少须执行下面的命令:
iptables
-t nat -A POSTROUTING -o ppp0 -j MASQUERADE
完成后,在192.168.0.0网段(网关为192.168.0.1)的PC机就可透过Linux上网了!
8、 开机自启动
为了使Linux服务器能够自动拨号,执行下面步骤。
1)chkconfig --add adsl
2)setup
3)选择system services
4)选中ADSL
5)OK退出
6)打开/etc/rc.d/rc.local,在该文件的末尾添上下面语句
echo
"[OK]"
echo
"Drop ICMP form anywhere"
echo
1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
echo
"[OK]"
iptables
-t nat -A POSTROUTING -o ppp0 -j MASQUERADE
说明:前面四句用于关闭ICMP,防止别人Ping。
9、 至此,一切OK,一个简单的拨号建成了。重启机器后,发现linux的internet共享连接已经一切就绪了,好妙!!!
为了建立更安全的拨号连接,请再设置各种安全机制吧,好事多磨嘛。
另外,如果网关后面的客户机无法通过linux上网,请留意一下linux的防火墙设置。
|
REDHAT9上ADSL最终解决方案
发布于2005-05-29 被读559次
【字体:大 中 小】
在LINUXSIR和LINUXFANS上看了很多关于ADSL的文章,都没有解决我的REDHAT9上ADSL上网的问题,今天实在是没有办法,重新建立连接,曲折的经历,终于上网了(非常激动,可能表达的不是很好),特的写下我的过程,作为参考:
REDHAT默认的PPPOE有问题,需要RPM -E,然后,安装这个 --实际在RedES3上没必要按照这个共享包。
http://www.roaringpenguin.com/pppoe/rp-pppoe-3.5.tar.gz(北南兄推荐)
解压和安装:
#tar zxvf rp-pppoe-3.5.tar.gz
进入解压目录执行
#sh ./go
然后再来设置ADSL。这一处,我们要用命令。
#adsl-setup
Welcome to the Roaring Penguin ADSL client
setup. First, I will run
some checks on your system to make sure the
PPPoE client is installed
properly...
Looks good! Now, please enter some
information:
USER NAME
>>> Enter your PPPoE user name
(default XXX): 在这里输入ADSL的用户名
INTERFACE
>>> Enter the Ethernet interface
connected to the ADSL modem
For Solaris, this is likely to be something
like /dev/hme0.
For Linux, it will be ethn, where 'n' is a
number.
(default eth0):如果一张网卡就设置写上eth0
Do you want the link to come up on demand,
or stay up continuously?
If you want it to come up on demand, enter
the idle time in seconds
after which the link should be dropped. If
you want the link to
stay up permanently, enter 'no' (two
letters, lower-case.)
NOTE: Demand-activated links do not
interact well with dynamic IP
addresses. You may have some problems with
demand-activated links.
>>> Enter the demand value
(default no):不用写什么
DNS
Please enter the IP address of your ISP's
primary DNS server.
If your ISP claims that 'the server will
provide DNS addresses',
enter 'server' (all lower-case) here.
If you just press enter, I will assume you
know what you are
doing and not modify your DNS setup.
>>> Enter the DNS information
here:在这里写上202.96.134.133
下一个DNS是202.96.168.68 //这里根据个人不同可以修改
PASSWORD
>>> Please enter your PPPoE
password:输入密码
>>> Please re-enter your PPPoE
password:再输入一次
FIREWALLING
Please choose the firewall rules to use.
Note that these rules are
very basic. You are strongly encouraged to
use a more sophisticated
firewall setup; however, these will provide
basic security. If you
are running any servers on your machine,
you must choose 'NONE' and
set up firewalling yourself. Otherwise, the
firewall rules will deny
access to all standard servers like Web,
e-mail, ftp, etc. If you
are using SSH, the rules will block
outgoing SSH connections which
allocate a privileged source port.
The firewall choices are:
0 - NONE: This script will not set any
firewall rules. You are responsible
for ensuring the security of your machine.
You are STRONGLY
recommended to use some kind of firewall
rules.
1 - STANDALONE: Appropriate for a basic
stand-alone web-surfing workstation
2 - MASQUERADE: Appropriate for a machine
acting as an Internet gateway
for a LAN
>>> Choose a type of firewall
(0-2):这里添写为2
** Summary of what you entered **
Ethernet Interface: eth0
User name: XXX
Activate-on-demand: No
DNS: Do not adjust
Firewalling: MASQUERADE
>>> Accept these settings and
adjust configuration files (y/n)?
弄完后,就按一个y键。
(以上为北南兄文章里面内容)
不要急于连接,REBOOT -N
然后进入网络设置,停止ETH1(我的是用他)
然后ADSL-START
PING 你的DNS,如果可以,那么,恭喜你!
其中部分内容可能不同,仅作参考,主要在连接后,能够PING通DNS即可!
==========================================================================
首先应该确定您是否安装了pppoe的应用程序。
如果确实已经安装了,可以在终端用 adsl-setup命令启动adsl配置,提示过程为英文。
大概为:
[root@localhost root]# adsl-setup
Welcome to the ADSL client setup. First, I
will run some checks on
your system to make sure the PPPoE client
is installed properly...
The following DSL config was found on your
system:
Device: Name:
ppp0 DSLppp0
Please enter the device if you want to
configure the present DSL config
(default ppp0) or enter 'n' if you want to
create a new one: ppp0 //默认为ppp0
LOGIN NAME
Enter your Login Name (default SJ00411210A1): anthrax //这里用你自己的用户名代替我的anthrax:)
INTERFACE
Enter the Ethernet interface connected to
the ADSL modem
For Solaris, this is likely to be something
like /dev/hme0.
For Linux, it will be ethX, where 'X' is a
number.
(default eth0): eth0 //默认网卡设备为eth0
Do you want the link to come up on demand,
or stay up continuously?
If you want it to come up on demand, enter
the idle time in seconds
after which the link should be dropped. If
you want the link to
stay up permanently, enter 'no' (two
letters, lower-case.)
NOTE: Demand-activated links do not
interact well with dynamic IP
addresses. You may have some problems with
demand-activated links.
Enter the demand value (default no): no //这里使用默认no就可以了,断线后不自动拨号。
DNS
Please enter the IP address of your ISP's
primary DNS server.
If your ISP claims that 'the server will provide
dynamic DNS addresses',
enter 'server' (all lower-case) here.
If you just press enter, I will assume you
know what you are
doing and not modify your DNS setup.
Enter the DNS information here:
202.96.134.133 //主DNS地址设置,根据您的具体情况替换。
Please enter the IP address of your ISP's
secondary DNS server.
If you just press enter, I will assume
there is only one DNS server.
Enter the secondary DNS server address
here: 202.96.134.133 //第二DNS地址设置。
PASSWORD
Please enter your Password: //这里设置密码,和unix规则一样,密码并不回显,因此不要认为您的键盘出了毛病:)
Please re-enter your Password:
//确认密码
USERCTRL
Please enter 'yes' (two letters,
lower-case.) if you want to allow
normal user to start or stop DSL connection
(default yes): yes //是否允许普通用户共享ADSL。
FIREWALLING
Please choose the firewall rules to use.
Note that these rules are
very basic. You are strongly encouraged to
use a more sophisticated
firewall setup; however, these will provide
basic security. If you
are running any servers on your machine,
you must choose 'NONE' and
set up firewalling yourself. Otherwise, the
firewall rules will deny
access to all standard servers like Web,
e-mail, ftp, etc. If you
are using SSH, the rules will block
outgoing SSH connections which
allocate a privileged source port.
The firewall choices are:
0 - NONE: This script will not set any
firewall rules. You are responsible
for ensuring the security of your machine.
You are STRONGLY
recommended to use some kind of firewall
rules.
1 - STANDALONE: Appropriate for a basic
stand-alone web-surfing workstation
2 - MASQUERADE: Appropriate for a machine
acting as an Internet gateway
for a LAN
Choose a type of firewall (0-2): 1 //配置防火墙等级,根据您的需要选择。
Start this connection at boot time
Do you want to start this connection at
boot time?
Please enter no or yes (default no):no //是否允许开机自动加载,这里选择no,否则系统启动速度太慢!
** Summary of what you entered **
Ethernet Interface: eth0
User name: anthrax
Activate-on-demand: No
Primary DNS: 202.96.134.133
Secondary DNS: 202.96.134.133
Firewalling: STANDALONE
User Control: yes
Accept these settings and adjust
configuration files (y/n)?
选择y,配置完成。您可以用 adsl-start命令启动,可以用adsl-stop命令停止。
为了方便,可以在桌面建立一个应用程序链接,命令就使用adsl-start。这样每次双击那个快捷图标就可以建立adsl链接了,跟windows中一样方便。
好了,现在就开始您的网络之旅吧。(技巧:如果依据本内容操作扔不能链接网络,可以尝试在"系统设置->网路"中删除当前的网卡,重新配置adsl项。)