2007年10月14日
一,做一个最简单的Applet:
1,编译JAVA类:
{TOMCAT_HOME} = D:\tomcat6
假设在D:\tomcat6\webapps\testweb\test目录中新建hello目录,在hello目录中新建java类HelloWorld.java:
package hello;
import java.awt.*;
import java.applet.*;
//必须继承Appelet类
public class HelloWorld extends Applet {
public void paint(Graphics g )
{
//向外输出的字符串
g.drawString("Hello HelloWorldd!",5,35);
}
}
在CMD命令行中编译该类:
C:\Documents and Settings\Administrator>d:
D:>cd D:\tomcat6\webapps\testweb\test
D:>javac hello\HelloWorld.java
2,在网页中嵌入Applet:
在D:\tomcat6\webapps\testweb\test目录中新建testHello.html文件:
<HTML>
<TITLE>HelloWorld! Applet</TITLE>
<APPLET
codebase="."
CODE="hello.HelloWorld.class"
name="HelloWorld"
WIDTH=200
HEIGHT=100
ALIGN = middle
>
</APPLET>
</HTML>
3,运行查看Applet:
appletviewer是一个专门用来运行查看applet的工具,直接在命令行上敲打就可以
在CMD命令行中
>d:
>cd D:\tomcat6\webapps\testweb\test
>appletviewer testHello.html
如果启动了TOMCAT,也可以在浏览器中浏览,不过有些浏览器不支持,需要JAVA插件才可以看到效果,比如遨游,我是在IE6.0中运行可以看到。
二,Applet进一步处理:
如果想把网页中的Applet标签转换成<Object>或者<Embed>形式,需要做以下工作,
4,将hello目录打包:
>d:
>cd D:\tomcat6\webapps\testweb\test
>jar -cvf hello.jar ./hello
5,使用htmlconverter工具来转换:
1)下载htmlconverter==>http://java.sun.com/products/plugin/1.3/docs/html_converter.html
2)下载后是一个压缩文件,解压缩后成为一个jar包,假设该jar包在D:\tomcat6\webapps\testweb\test下
>d:
>cd D:\tomcat6\webapps\testweb\test
>java -jar htmlconv1_3.jar helloworld.jsp (注:不清楚htmlconverter怎么用的,可以敲命令查看帮助:java -jar htmlconv1_3.jar -help)
此时helloworld.jsp中的代码将被转换成如下样式:
<HTML>
<TITLE>HelloWorld! Applet</TITLE>
<!--"CONVERTED_APPLET"-->
<!-- HTML CONVERTER -->
<OBJECT classid="clsid:E19F9331-3110-11d4-991C-005004D3B3DB"
WIDTH = 200 HEIGHT = 100 NAME = "HelloWorld" ALIGN = middle codebase="http://java.sun.com/products/plugin/1.3.0_02/jinstall-130_02-win32.cab#Version=1,3,0,2">
<PARAM NAME = CODE VALUE = "hello.HelloWorld.class" >
<PARAM NAME = CODEBASE VALUE = "." >
<PARAM NAME = ARCHIVE VALUE = "HelloWorld.jar" >
<PARAM NAME = NAME VALUE = "HelloWorld" >
<PARAM NAME="type" VALUE="application/x-java-applet;jpi-version=1.3.0_02">
<PARAM NAME="scriptable" VALUE="false">
<COMMENT>
<EMBED type="application/x-java-applet;jpi-version=1.3.0_02" CODE = "hello.HelloWorld.class" CODEBASE = "." ARCHIVE = "HelloWorld.jar" NAME = "HelloWorld" WIDTH = 200 HEIGHT = 100 ALIGN = middle scriptable=false pluginspage="http://java.sun.com/products/plugin/1.3.0_02/plugin-install.html"><NOEMBED>
</NOEMBED>
</EMBED>
</COMMENT>
</OBJECT>
<!--
<APPLET CODE = "hello.HelloWorld.class" CODEBASE = "." ARCHIVE = "HelloWorld.jar" WIDTH = 200 HEIGHT = 100 NAME = "HelloWorld" ALIGN = middle>
</APPLET>
-->
<!--"END_CONVERTED_APPLET"-->
</HTML>
这样转换就已经完成,此时再用appletviewer去查看,效果和前面一致
posted @
2007-12-12 13:19 诗语江南 阅读(4703) |
评论 (0) |
编辑 收藏
本文转自BeanSoft:
http://www.blogjava.net/beansoft/archive/2007/10/09/151368.html
常见的 Tomcat 5 表单编码提交后乱码的解决方案有很多, 有改 Tomcat Connector 属性的, 有加过滤器的. 但是 Tomcat 自带的那个过滤器只解决了 POST 方式的乱码却不能处理 GET 方式的乱码. 在这里那我就给出一个不需要修改任何 Tomcat 配置文件的完美解决中文问题的过滤器的代码:
首先是我们的测试页面:
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>表单测试页面</title>
</head>
<body>
<form action="action.jsp">
GET 方式: <input type=text name="name" value="GET 中文">
<input type=submit>
</form>
<form action="action.jsp" method="POST">
POST 方式: <input type=text name="name" value="POST 中文">
<input type=submit>
</form>
</body>
</html>
和普通的表单没有任何区别.
然后就是表单提交页面 action.jsp:
<%@ page language="java" pageEncoding="GBK"%>
<html>
<body>
参数: <%=request.getParameter("name") %> <br>
</body>
</html>
接着是过滤器的配置文件 web.xml (用 2.5 版本也可以):
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>TomcatFormFilter</filter-name>
<filter-class>filters.TomcatFormFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>TomcatFormFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
最后最关键的就是我们的过滤器代码了, 对 GET 方式使用请求包装器, 而 POST 方式则用 setCharacterEncoding():
package filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
public class TomcatFormFilter implements Filter {
/**
* Request.java
* 对 HttpServletRequestWrapper 进行扩充, 不影响原来的功能并能提供所有的 HttpServletRequest
* 接口中的功能. 它可以统一的对 Tomcat 默认设置下的中文问题进行解决而只需要用新的 Request 对象替换页面中的
* request 对象即可.
*/
class Request extends HttpServletRequestWrapper
{
public Request(HttpServletRequest request) {
super(request);
}
/**
* 转换由表单读取的数据的内码.
* 从 ISO 字符转到 GBK.
*/
public String toChi(String input) {
try {
byte[] bytes = input.getBytes("ISO8859-1");
return new String(bytes, "GBK");
}
catch (Exception ex) {
}
return null;
}
/**
* Return the HttpServletRequest holded by this object.
*/
private HttpServletRequest getHttpServletRequest()
{
return (HttpServletRequest)super.getRequest();
}
/**
* 读取参数 -- 修正了中文问题.
*/
public String getParameter(String name)
{
return toChi(getHttpServletRequest().getParameter(name));
}
/**
* 读取参数列表 - 修正了中文问题.
*/
public String[] getParameterValues(String name)
{
String values[] = getHttpServletRequest().getParameterValues(name);
if (values != null) {
for (int i = 0; i < values.length; i++) {
values[i] = toChi(values[i]);
}
}
return values;
}
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpreq = (HttpServletRequest)request;
if(httpreq.getMethod().equals("POST")) {
request.setCharacterEncoding("GBK");
} else {
request = new Request(httpreq);
}
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
怎么样, 是不是很简单也很方便呢?
posted @
2007-10-14 22:16 诗语江南 阅读(434) |
评论 (0) |
编辑 收藏