JBuilder9下使用Struts1.1的方法
星期一, 七月 19th, 2004将Struts1.1目录下的lib文件拷贝到{jbuilder9-home}/extras/jakarta-struts1.1/目录下
JBuilder9添加一个用户定义的library命名为Struts1.1,并添加上一目录里的所有jar文件
然后在library setting中选择framework选择Struts
DONE
将Struts1.1目录下的lib文件拷贝到{jbuilder9-home}/extras/jakarta-struts1.1/目录下
JBuilder9添加一个用户定义的library命名为Struts1.1,并添加上一目录里的所有jar文件
然后在library setting中选择framework选择Struts
DONE
主 题: 分享一下,linux下在servlet中使用java.awt.*对象的解决办法
作 者: MarsZ (夜夜夜夜)
等 级:
信 誉 值: 94
所属论坛: Java JSP/Servlet/JavaBean
问题点数: 0
回复次数: 5
发表时间: 2004-03-29 15:57:04
前几天自己碰上的一个问题
代码和Exception如下:
/**
* AuthorityGenerator.java
*/
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import Acme.JPM.Encoders.GifEncoder;
public class AuthorityGenerator extends HttpServlet {
private static final String CONTENT_TYPE = “image/gif”;
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletOutputStream out = response.getOutputStream();
Frame frame = null ;
Graphics g = null;
Random random = new Random();
/* background RGB */
int bgr = Integer.parseInt(request.getParameter(“bgr”));
int bgg = Integer.parseInt(request.getParameter(“bgg”));
int bgb = Integer.parseInt(request.getParameter(“bgb”));
/* foreground RGB */
int fgr = Integer.parseInt(request.getParameter(“fgr”));
int fgg = Integer.parseInt(request.getParameter(“fgg”));
int fgb = Integer.parseInt(request.getParameter(“fgb”));
String text = request.getParameter(“text”);
String fontName = request.getParameter(“fontName”);
int fontSize = Integer.parseInt(request.getParameter(“fontSize”));
try{
frame = new Frame();
frame.addNotify();
Image image = frame.createImage(100, 100);
g = image.getGraphics();
g.setColor(new Color(bgr, bgg, bgb));
g.fillRect(0, 0, image.getWidth(frame), image.getHeight(frame));
for(int i=0; i<image.getWidth(frame);i++){
int x = random.nextInt(100);
int y = random.nextInt(100);
g.setColor(new Color(random.nextInt()));
g.fillRect(x, y, 1, 1);
}
g.setColor(new Color(fgr, fgg, fgb));
g.setFont(new Font(fontName, Font.PLAIN, fontSize));
g.drawString(text, 2, 100);
response.setContentType(CONTENT_TYPE);
GifEncoder encoder = new GifEncoder(image, out);
encoder.encode();
}finally{
if(g!=null) g.dispose();
if(frame != null) frame.removeNotify();
}
}
//Clean up resources
public void destroy() {
}
}
/**
* exceptions
*/
java.lang.NoClassDefFoundError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:141)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:62)
at java.awt.Window.init(Window.java:231)
at java.awt.Window.<init>(Window.java:275)
at java.awt.Frame.<init>(Frame.java:401)
at java.awt.Frame.<init>(Frame.java:366)
at listener_test.AuthorityGenerator.doGet(AuthorityGenerator.java:46)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:126)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:103)
at com.caucho.server.http.FilterChainServlet.doFilter(FilterChainServlet.java:96)
at com.caucho.server.http.Invocation.service(Invocation.java:315)
at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
at com.caucho.server.http.RunnerRequest.handleRequest(RunnerRequest.java:346)
at com.caucho.server.http.RunnerRequest.handleConnection(RunnerRequest.java:274)
at com.caucho.server.TcpConnection.run(TcpConnection.java:139)
at java.lang.Thread.run(Thread.java:534)
网上找了很久,很多类似问题但没有得到解决,基本上都是说加入系统环境变量
System.setProperty(“java.awt.headless”, “true”);
但也无效,最后解决的办法是首先linux要安装xwindow并启动,然后在系统环境变量中加入DISPLAY(这个是Xserver的地址)
> vi /etc/profile
加入
DISPLAY=127.0.0.1:0.0
export DISPLAY
然后就没问题了
回复人: ganh(水水水) ( ) 信誉:105 2004-03-31 15:10:00 得分:0
你这个还是要安装 xwindows ,并不是很好的方法。加入系统环境变量的方法你们用错了。
我记得还是我最早在csdn中提出这个问题并解决了。当初也是到处找资料,最后在sun的bug列表中找到的。我的解决方法如下。
1:将jdk升级为jdk1.4,然后在java命令后加上运行参数 –Djava.awt.headless=true 即可,java xxxx.class -Djava.awt.headless=true 。
2:虚拟一个图形环境。一般在linux下使用Xvfb来虚拟一个图形环境,在linux命令行键入Xvfb :0 -screen 0 1024x768x16 & ,不过在使用之前要对Xvfb进行设置。我在本机上没有调试成功。各位可以试一下。
3:使用第三方工具包。可以使用 pja包,下载地址 http://www.eteks.com/pja/en/#Download ,使用方法见解决方案。
解决方案:将pja.jar ,pjatools.jar拷贝到weblogic的lib目录下,在startWebLogic.sh中将这两个文件加入到CLASSPATH中,然后将以下文字加入到$JAVACMD中,其中
/usr/java/jdk1.3.1_06 是指java的安装目录
/home/weblogic/wlserver6.1/lib 是指 pja.jar ,pjatools.jar的存放目录
-Xbootclasspath/a:/usr/java/jdk1.3.1_06/lib/rt.jar:/home/weblogic/wlserver6.1/lib/pja.jar -Djava.awt.toolkit=com.eteks.awt.PJAToolkit -Djava.awt.graphicsenv=com.eteks.java2d.PJAGraphicsEnvironment -Djava.awt.fonts=/usr/java/jdk1.3.1_06/jre/lib/fonts
添加后执行会出现一些说 font找不到的提示,不用理他。
讨论:这个问题在java中称为headless问题,是jdk1.3的bug,bug报告见http://developer.java.sun.com/developer/bugParade/bugs/4281163.html 。jdk1.4已经做了修正,文档见http://java.sun.com/j2se/1.4.1/docs/guide/awt/AWTChanges.html。
我对这个问题的理解:headless是指由于一些服务器(比如大型机)运行的环境比较特殊,不存在真实的设备去处理一些输入输出,比如显卡,键盘鼠标;这时就需要虚拟一些运行环境出来,或者就不要用到这些设备的相关操作(比如输入输出)。然而运行java.awt包又需要一个的图形环境,由此产生了异常。
回复人: MarsZ(夜夜夜夜) ( ) 信誉:94 2004-04-01 10:05:00 得分:0
thx, 我想启动xwindow也不是个好办法,但只通过加运行时加-Djava.awt.headless=true在application得时候顶用,但在servlet得时候依然报错,准备试试2、3种方法
回复人: MarsZ(夜夜夜夜) ( ) 信誉:94 2004-04-01 11:47:00 得分:0
哭了pja不支持j2se1.4.1
PJA supports JDK 1.4.0 but doesn’t support JDK 1.4.1 at this time (unless you directly instantiate PJAImage class and don’t use java.awt.Font class). As the exception suggests, the main reason is that Sun added the following method in sun.java2d.SunGraphicsEnvironment class :
protected abstract sun.awt.FontProperties createFontProperties();
and this method isn’t implemented in com.eteks.java2d.PJAGraphicsEnvironment.
The sun.awt.FontProperties object this method is supposed to return is a JDK 1.4.1 new class that has plenty of remarks :
REMIND: remove this method and references to it from the next feature release
You can guess, this isn’t really motivating with such remarks to modify com.eteks.java2d.PJAGraphicsEnvironment to allow it to support JDK 1.4.1 !
Do you really need support for JDK 1.4.1 or are you just trying it ?
By the way, did you try to set the java.awt.headless System property to true and see if the new headless system works for your classes without PJA ?
复人: ganh(水水水) ( ) 信誉:105 2004-04-01 12:56:00 得分:0
在servlet中也是可以使用-Djava.awt.headless=true 的。要针对不同的jsp服务器进行设置。如果使用weblogic的话,在Weblogic启动脚本中的$JAVACMD $JAVA_OPTIONS 后面加入-Djava.awt.headless=true 就可以了。几乎所有的中间件服务器都是使用java编写的,服务器启动时就是需要运行java的。tomcat 也试过,没有问题。
回复人: MarsZ(夜夜夜夜) ( ) 信誉:94 2004-04-01 14:39:00 得分:0
嗯我是白痴。。我在servlet里得init方法中加了
System.setProperty(“java.awt.headless”, “true”);
结果不行
用得Resin,其实在resin的httpd.sh中把
args=”-Djava.awt.headless=true”
加上就行了
没什么感慨了,也不想再发牢骚,干完这票买卖再说吧
1. gzip -d httpd-2_0_NN.tar.gz
2. tar xvf httpd-2_0_NN.tar
3. gunzip php-NN.tar.gz
4. tar -xvf php-NN.tar
5. cd httpd-2_0_NN
6. ./configure –enable-so
7. make
8. make installNow you have Apache 2.0.NN available under /usr/local/apache2,
configured with loadable module support and the standard MPM prefork.
To test the installation use your normal procedure for starting
the Apache server, e.g.:
/usr/local/apache2/bin/apachectl start
and stop the server to go on with the configuration for PHP:
/usr/local/apache2/bin/apachectl stop.9. cd ../php-NN
10. Now, configure your PHP. This is where you customize your PHP
with various options, like which extensions will be enabled. Do a
./configure –help for a list of available options. In our example
we’ll do a simple configure with Apache 2 and MySQL support. Your
path to apxs may differ, in fact, the binary may even be named apxs2 on
your system../configure –with-apxs2=/usr/local/apache2/bin/apxs –with-mysql
11. make
12. make installIf you decide to change your configure options after installation,
you only need to repeat the last three steps. You only need to
restart apache for the new module to take effect. A recompile of
Apache is not needed.Note that unless told otherwise, ‘make install’ will also install PEAR,
various PHP tools such as phpize, install the PHP CLI, and more.13. Setup your php.ini
cp php.ini-dist /usr/local/lib/php.ini
You may edit your .ini file to set PHP options. If you prefer having
php.ini in another location, use –with-config-file-path=/some/path in
step 10.If you instead choose php.ini-recommended, be certain to read the list
of changes within, as they affect how PHP behaves.14. Edit your httpd.conf to load the PHP module. The path on the right hand
side of the LoadModule statement must point to the path of the PHP
module on your system. The make install from above may have already
added this for you, but be sure to check.For PHP 4:
LoadModule php4_module libexec/libphp4.so
For PHP 5:
LoadModule php5_module libexec/libphp5.so
15. Tell Apache to parse certain extensions as PHP. For example,
let’s have Apache parse the .php extension as PHP. You could
have any extension(s) parse as PHP by simply adding more, with
each separated by a space. We’ll add .phtml to demonstrate.AddType application/x-httpd-php .php .phtml
It’s also common to setup the .phps extension to show highlighted PHP
source, this can be done with:AddType application/x-httpd-php-source .phps
16. Use your normal procedure for starting the Apache server, e.g.:
/usr/local/apache2/bin/apachectl start
首先确认在系统里安装了jdk1.4以上版本,并设置了$JAVA_HOME环境变量
安装tomcat
解压jakarta-tomcat-5.0.19.tar.gz
> tar zxvf jakarta-tomcat-5.0.19.tar.gz
把解压后的目录拷贝到/opt/tomcat目录下,建立连接:
>ln -s /opt/tomcat /usr/local/tomcat
添加tomcat用户并设置密码
>groupadd tomcat
>useradd -g tomcat -c “Tomcat User” -d /usr/local/tomcat tomcat
>passwd tomcat
向环境变量中添加$CATALINA_HOME
CATALINA_HOME=/usr/local/tomcat
export CATALINA_HOME
启动
>su – tomcat -c /usr/local/tomcat/bin/startup.sh
访问http://youripaddress:8080,查看是否成功,未成功请检查以上步骤
停止
>su – tomcat -c /usr/local/tomcat/bin/shutdown.sh
安装apache
>./configure –prefix=/usr/local/apache –enable-ssl –enable-so
>make
>make install
如果install时失败请手动安装openssl,并把openssl的include目录指向到/usr/include/openssl
启动
>/usr/local/apache/bin/apachectl start
访问http://youripaddress,查看是否成功,未成功请检查以上步骤
停止
>/usr/local/apache/bin/apachectl stop
编译并安装JK2
下载JK2源代码,并解压
编译
>cd jakarta-tomcat-connectors-jk2-2.0.2-src
>cd jk/native2
>chmod 755 buildconf.sh
>./configure –with-apxs2=/usr/local/apache/bin/apxs –enable-EAPI
>make
>make install
如果install 失败手动拷贝mod_jk2.so到apache的modules目录
>cd /usr/local/src/jakarta-tomcat-connectors-jk2-2.0.2-src/jk/build/jk2/apache2
>cp *.so /usr/local/apache/modules/
整合
编辑/usr/local/apache/conf/http.conf文件,加入:
LoadModule jk2_module modules/mod_jk2.so
编辑/usr/local/tomcat/conf/jk2.properties文件,内容如下:
# list of needed handlers.
handler.list=channelSocket,request
# Override the default port for the channelSocket
channelSocket.port=8009
编辑/usr/local/apache/conf/workers2.properties文件,内容如下:
# only at beginnin. In production uncomment it out
[logger.apache2]
level=DEBUG
[shm]
file=/usr/local/apache/logs/shm.file
size=1048576
# Example socket channel, override port and host.
[channel.socket:localhost:8009]
port=8009
host=127.0.0.1
# define the worker
[ajp13:localhost:8009]
channel=channel.socket:localhost:8009
# Uri mapping
[uri:localhost/*]
worker=ajp13:localhost:8009
完成
启动tomcat
启动apache
访问http://youripaddress/index.jsp文件查看是否正确
如果对jk2.properties和workers.properties的有进一步理解,请看tomcat关于jk2的文档http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jk2/index.html
首先需要安装soap包
soap还需要javamail和javabeans activation framework,分别在以下地址下载
http://apache.linuxforum.net/dist/ws/soap/
http://java.sun.com/products/javamail/downloads/index.html
http://java.sun.com/products/javabeans/glasgow/jaf.html
把以上soap包中的soap.war放入到{$tomcathome}/webapps目录下,soap.war自动deploy,把mail.jar和activation.jar放到系统的classpath和tomcat的lib中,访问
http://localhost:8080/soap/servlet/rpcrouter
没有报错则成功
创建Echo.java
代码如下
public class Echo
{
public String echo(String echome){
return echome;
}
}
同一目录下创建DeployDescriptor.xml
代码如下
<isd:service xmlns:isd=”http://xml.apache.org/xml-soap/deployment” id=”urn:Echo”>
<isd:provider type=”java” scope=”Application” methods=”echo”>
<isd:java class=”Echo” static=”false”/>
</isd:provider>
</isd:service>
Deploy SOAP服务
命令行执行
>java org.apache.soap.server.ServiceManagerClient http://localhost:8080/soap/servlet/rpcrouter deploy DeploymentDescriptor.xml
检查服务是否安装
>java org.apache.soap.server.ServiceManagerClient http://localhost:8080/soap/servlet/rpcrouter list
然后把编译过的Echo.java放置到{$tomcathome}/webapps/soap/WEB-INF/classes目录下
创建SOAP客户端程序
代码如下
import java.io.*;
import java.util.*;
import java.net.*;
import org.w3c.dom.*;
import org.apache.soap.util.xml.*;
import org.apache.soap.*;
import org.apache.soap.encoding.*;
import org.apache.soap.encoding.soapenc.*;
import org.apache.soap.rpc.*;
public class TestEchoService {
public static void main(String[] args) throws Exception{
Call call = new Call();
call.setTargetObjectURI(“urn:Echo”);
call.setMethodName(“echo”);
call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
Vector params = new Vector();
params.addElement(new Parameter(“value”, String.class, args[0], null));
call.setParams(params);
Response resp;
try{
resp = call.invoke(new URL(“http://localhost:8080/soap/servlet/rpcrouter“),
“”);
if (!resp.generatedFault()) {
Parameter ret = resp.getReturnValue();
Object value = ret.getValue();
System.out.println(value);
}else{
Fault fault = resp.getFault();
System.err.println(“Generated fault:”);
System.out.println(“ Fault Code = ” + fault.getFaultCode());
System.out.println(“ Fault String = ” + fault.getFaultString());
}
}catch(SOAPException e){
System.out.println(“Caught SOAPException(” +
e.getFaultCode()+”):”+
e.getMessage());
}
}
}
编译并执行:
>java TestEchoService 日鸡鸡
完成
第一步,点击这里下载jk2 connector,解压到{Tomcat_Home}/conf/connector 目录下
第二步,打开IIS控制台,选择网站属性,ISAPI筛选器->添加,名称添加jakarta,执行文件找到isapi_redirector2.dll确定;增加一名为jakarta的虚拟路径指向{Tomcat_Home}/conf/connector,设置为“脚本和可执行文件”
第三步,编辑workers2.properties文件内容可能如下
[shm]
# 修改成向对应你的路径
file=d:/Tomcat/logs/jk2.log
size=1048576
# Example socket channel, override port and host.
[channel.socket:localhost:8009]
# 这里对应Server.xml里面定义的端口
# 如果是默认设置不要更改
port=8009
host=127.0.0.1
# define the worker
[ajp13:reynir_net:8009]
channel=channel.socket:localhost:8009
# Uri mapping
# 映射路径
# 这里是Tomcat下面的路径映射到IIS的方法
# 需要把哪个目录放到IIS下就要写几个
[uri:/*.jsp]
[uri:/jsp-examples/*]
[uri:/my/*]
worker=ajp13:localhost:8009
# define the worker
[status:status]
# Uri mapping
[uri:/jkstatus/*]
worker=status:status
第四步,编辑isapi_redirect.2.0.reg文件,内容可能如下
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\Jakarta Isapi Redirector]
[HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\Jakarta Isapi Redirector\2.0]
“serverRoot”=”d:\\Tomcat”
“extensionUri”=”/jakarta/isapi_redirector2.dll”
“workersFile”=”d:\\Tomcat\\conf\\connector\\workers2.properties”
“logLevel”=”DEBUG”
几个路径改成相对应你的系统下的路径
第五步,在IIS下添加虚拟目录,比如指向”D:\Tomcat\webapps\jsp-examples”(添加的这个路径必须在workers2.properties里面的映射定义过),路径名为”jsp-examples”
最后,重启tomcat和IIS(要重启整个www服务),ok现在访问 “http://localhost/jsp-examples”看看是不是出来了
server.xml定义上下文,<Host>标签中
<Context path=”/publish” docBase=”publish” debug=”5″ reloadable=”true” crossContext=”true”>
<Resource name=”jdbc/publish_sqlserver” auth=”SERVLET” type=”javax.sql.DataSource”/>
<ResourceParams name=”jdbc/publish_sqlserver”>
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<!– Maximum number of dB connections in pool. Make sure you
configure your mysqld max_connections large enough to handle
all of your db connections. Set to 0 for no limit.
–>
<parameter>
<name>maxActive</name>
<value>100</value>
</parameter>
<!– Maximum number of idle dB connections to retain in pool.
Set to 0 for no limit.
–>
<parameter>
<name>maxIdle</name>
<value>30</value>
</parameter>
<!– Maximum time to wait for a dB connection to become available
in ms, in this example 10 seconds. An Exception is thrown if
this timeout is exceeded. Set to -1 to wait indefinitely.
–>
<parameter>
<name>maxWait</name>
<value>1000</value>
</parameter>
<!– MySQL dB username and password for dB connections –>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>secret</value>
</parameter>
<!– Class name for mm.mysql JDBC driver –>
<parameter>
<name>driverClassName</name>
<value>com.jnetdirect.jsql.JSQLDriver</value>
</parameter>
<!– The JDBC connection url for connecting to your MySQL dB.
The autoReconnect=true argument to the url makes sure that the
mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
connection. mysqld by default closes idle connections after 8 hours.
–>
<parameter>
<name>url</name>
<value>jdbc:JSQLConnect://192.168.1.108:1433/database=repent/applicationName=sWPS2.0</value>
</parameter>
</ResourceParams>
</Context>
web application的web.xml中定义使用的资源
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/publish_sqlserver</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
redeploy web application后,如果数据库参数没有错误,可以按如下方法使用
<%@page import=”java.util.*”%>
<%@page import=”javax.naming.*”%>
<%@page import=”javax.sql.*”%>
<pre>
<%
Context initialcontext = new InitialContext();
Context envContext = (Context)initialcontext.lookup(“java:comp/env”);
DataSource dataSource = (DataSource)envContext.lookup(“jdbc/publish_sqlserver”);
Connection connection = null;
Statement stmt = null;
if(dataSource!=null)
connection = dataSource.getConnection();
if (connection != null) {
stmt = connection.createStatement();
}
connection.close();
stmt.close();
%>
</pre>