Use of Resin server

Source: Internet
Author: User
1. Introduction to Java Web Server Selection

In actual implementation of java Web projects, we can use commercial java Web servers such as Ibm WebSphere and Bea Web Logic. These two servers have complete functions and are powerful. They support all java service container standards and are suitable for the release of commercial java Web applications. However, these two types of servers are commercial servers, which are expensive and require extremely high system resources. In particular, the Websphere configuration is complex. It is not suitable for development if you do not use ibm Websphere Studio Application. In addition, different versions have different requirements for servlet. jar and other javax components and jdk. For example, servlet. jar required by Websphere 3.5 supports the old version of javax. servlet. http. HttpSession, and does not support session. setAttribute () session. getAttribute ().

In the development or learning process, there are many free and lightweight Java Web servers for us to choose from, such as Tomcat, Resin, Orion and so on. They are easy to use and consume few resources. They are suitable for continuous debugging during development. They can also be used with integrated development tools such as Jbuilder. Based on the actual development, Jakata Tomcat and Sun Java are the best combination. They can be used with other application servers to support the complete j2ee standard and are widely used. However, from the general coding problems in Java, Resin is the best solution. From the perspective of usage, I personally think that Resin is more convenient than Tomcat, and Resin can also be used in combination with ide such as Jbuilder6. Resin emphasizes the use of Xml technology, which can be seen from Resin's own HomePage using xtp.

Ii. installation and configuration of Resin

Resin is available for free download and use in http://www.caucho.com/download/index.xtp. Developing or learning with Resin is free of charge, but releasing Resin as a paid product requires payment. The current version is 2.10.

Local download:
Http://www.javafan.net/softview.jsp? ID = 26 Resin 2.1.8
Http://www.javafan.net/softview.jsp? Id = 159 resin 3.0.8

When downloading resin, select resin-2.1.0.zip from windows.zipin archive versions.

Decompress the zip package to any directory, such as D:/resin. The following describes how to install Resin in D:/resin. Enter D:/resin/bin and type httpd. You can run the Resin server on the command line console. A dialog box with the start and stop radio buttons and the close button is displayed. The running information of resin is displayed here. This information can also be viewed in the stdout. log file under the D:/resin/log directory. For example:

Resin 2.0.4 (built Thu Nov 15 17:56:24 PST 2001)
Copyright (c) 1998-2001 Caucho Technology. All rights reserved.
Starting Resin on Wed, 23 Jan 2002 14:41:47 + 0800 (CST)

HTTP listening to *: 80
Srun listening to haitaiserver: 6802

Click stop to stop the current Resin server process. Click start to start a new Resin server process. Close this dialog box and return to the drive letter prompt status on the Command console. If you want to use Resin as a service in nt4 or win2k, you only need to enter httpd in the directory of the Command console? Install, you can see an automatic Resin Web Server service added under the management tool service. You can start the Resin service as long as you enter nt 4 or win2k. This service can also be set to manual or disabled like other services. Note that after installing the service, start Resin. You cannot see that the Resin routine you have written can be parsed. the browser shows that the server cannot find the error. You only need to restart win2k or nt to solve the problem.

The following describes the configuration of the Resin server. The Resin server is configured using an Xml file like most Java Web servers.

Go to the d:/resin/conf directory and open resin. conf, which is a text in xml format. There are many tags here. First, find them:

Search for the

Iii. Introduction to Resin

When using Resin to develop Java Web projects, you must create your own WebApp. Here we will not introduce the development and use of Resin Cmp/Ejb, but will only introduce the use of Resin to develop common jsp/java servlet projects. Here we will also talk about the resin. conf configuration. Applications in Resin can be published in two ways: publishing in the Resin directory and publishing in a War package.

1. Publish in the Resin directory

Find the <web-app> tag in resin. conf, which indicates a web application.

The ID attribute indicates the web path of the application. For example, <web-app id = '/test'> indicates that the application should be accessed through http: // hostname/test/during web access. The app-Dir attribute indicates the actual path of the application. For example, <app-dir> D:/resin/doc/test </APP-dir> indicates that the application is under the D:/resin/doc/test directory. The default value is the directory with the same name as the ID under the root. Resin can be configured with three types of error-page: 404 error, that is, the file cannot find the error page; Exception violation page; cannot connect to the Java engine page. They can be set in this way respectively.

404 page not found for file

<Web-app id = '/app1'>
<Error-page error-code = '000000' location = '/file_not_found.jsp'/>
</Web-app>

Exception page

<Web-app id = '/foo'>
<Error-page exception-type = 'java. Lang. nullpointerexception' location = '/nullpointer. jsp'/>
</Web-app>

Cannot connect to the srun servlet engine error page

The settings on this page are irrelevant to the application and belong to the server settings.

<Http-Server>
<Error-page exception-type = 'connection' location = '/missing_file.html'/>
</HTTP-Server>

Classpath settings

See the following statement:

<Classpath id = 'web-INF/classes 'source = 'web-INF/src' compile = 'true'/>

The value of the id parameter indicates the storage path of the compiled class in classpath; the value of the source parameter indicates the storage path of the java source code in classpath; the value in compile may be true or false, indicates whether the java source code is automatically compiled by the Resin srun. The Classpath settings are generally related to the use of javaBean or Servlet. The value of id indicates the root of the compiled package of the javaBean, and the value of source indicates the root of the java source code of the javaBean. The Servlet is the same.

Servlet settings

See the following statement:

<Servlet-mapping url-pattern = '*. xtp' servlet-name = 'xtp '/>
<Servlet-mapping url-pattern = '*. jsp' servlet-name = 'jsp '/>
<Servlet-mapping url-pattern = '/servlet/*' servlet-name = 'invoker '/>

Generally, it is to specify the resolutions that need to pass the srun. For example, if you change *. jsp to *. jss, and the others remain unchanged, as long as you encounter *. jss files during access, they will be processed like the original *. jsp files. This allows you to specify the parsing engine, as shown in the following Configuration:

<Servlet-mapping url-pattern = '*. xtp' servlet-name = 'com. caucho. jsp. XtpServlet '/>

In Servlet, you can also specify servlet. For example

<Servlet-name = 'helloworld' servlet-class = 'test. helloworld'/>
<Servlet-mapping url-pattern = '/hello.html' servlet-name = 'hello'/>

When servlet-mapping has an important parameter case-sensitive in windows, it is recommended to set it to false to ignore case sensitivity, so as to be consistent with the windows conventions.

Session Configuration

See the following configuration statement:

<Session-config>
<Sessions-max> 4096 </session-max>
<Session-timeout> 30 </session-timeout>
<Enable-cookies> true </enable-cookies>
<Enable-url-rewriting> true </enable-url-rewriting>
<File-store> WEB-INF/sessions </file-store>
</Session-config>

Session-max: Maximum number of sessions
Session-timeout: The expiration time of the session, in minutes.
Cookie: whether the session uses cookies. If cookies are used, the browser must support the session before using them. We recommend that you change the value to false when publishing. Enable-url-rewriting and enable-cookies are generally used together. If enable-cookies are false,
Enable-url-rewriting should be set to true.
File-store: indicates whether the server stores the session as a file on the server. If this option is commented out, the serialized session object is not saved in the web-Inf/sessions directory under your WEB-app directory. The jdbc-store configuration also exists in the Session, which stores the session permanently in the database through jdbc. It is actually the physical implementation of saving and reloading session variables after serialization.

Here, session also supports multi-server settings through tcp-store parameter settings. Due to the load balancing problem, I will not describe it in detail here. I just want to write an example:

<Http-server>

<Http id = 'A' port = '80'/>
<Srun id = 'A' host = 'host-a' port = '000000'/>

<Http id = 'B' port = '80'/>
<Srun id = 'B' host = 'host-B 'port = '000000'/>

<Host id = ''>
<Web-app id = ''>

<Session-config>
<Tcp-store/>
<Always-load-session/>
</Session-config>
</Web-app>
</Host>

</Http-server>

In this example, the session is transmitted in the tcp ring mode.

Temp-dir settings

Temp-Dir refers to the temporary directory of the application. That is, the directory used in javax. servlet. Context. tempdir. The WEB-INF/tmp directory under the application directory.

The preceding settings can be set in the <web-app> tag center to control the settings of a Web application.

2. Package the package into war for release

The following describes how to publish a Java Web application packaged as war in resin.

In fact, this is the simplest and clearest good method. In J2EE, all projects are packaged into an ear for release. The Web application is packaged as war, And the EJB application is packaged as jar. In resin, these can be directly deployed. Here I will only introduce the deployment of Web applications packaged as war.

In resin. conf, find this: <war-Dir id = 'webapps'/>. It indicates the path to which the war file should be copied. This indicates the installation path relative to resin. For example, the preceding setting indicates D:/resin/webapps. You only need to restart resin. Resin automatically unpacks the war file to the webapps directory. You can see statements similar to [09:56:21. 680] initializing application http: // haitaiserver: 8080/rwtest in the command console or stdout. log. This indicates that the Web application is automatically installed. As long as this application complies with the J2EE standard web application, there should be no problem. You can access the application through the path shown above. If you browse in D:/resin/webapps/rwtest, you will see that resin has generated the rwtest directory for you, below are META-INF and WEB-INF as well as your own JSP/servlet files and directories. Is fully compliant with the J2EE structure. You can create a new JSP/servlet in the rwtest Directory, which can also be compiled, parsed, and run. In practice, you can use JBuilder, websphere, and other ide tools for integration debugging and packaging, which is very convenient.

4. Use Resin for java Web project development and debugging

The length here is limited and it is impossible to talk too much about it. I will only introduce the most useful part in practice.

If an error page is defined in resin, the most common exception after an error is not seen, and you can directly jump to the error page. Therefore, we recommend that you do not set error pages in development. The most common JSP errors are nullpoint exception, followed by name spelling errors. Errors can also be found in stderr. log in the log directory under the resin installation directory. Through analysis of the log file, you can see a lot of useful error messages.

When debugging JSP, if compile is defined as true, JSP is first translated into a servlet Java file and then compiled into a class file. You can find this file in your work directory. The Java name is defined in resin as follows: the original JSP file name is prefixed with an underscore and _ JSP is added. Therefore, names in Java applications should not end with _ JSP or contain Chinese characters. In fact, names starting with _ JSP are invalid.

The problem of Java's support for multiple languages has been well solved in resin. Take JSP as an example. Refer to the Java Servlet File automatically generated by resin. Add the following content at the beginning of any JSP file:

<% @ Page contenttype = "text/html; charset = gb2312" %>

The Chinese problem is solved. View the generated Servlet Source File segment:

Response. setcontenttype ("text/html; charset = gb2312 ");
Request. setcharacterencoding ("gb2312 ");

Set the character set.

Private Static byte [] _ jsp_string26;
Private Static byte [] _ jsp_string27;

_ Jsp_string26 = "/R/n </table>/R/n <Table class =/" type/">/R/n <tr>/R/n <TD> ". getbytes ("gb2312 ");
_ Jsp_string27 = "/R/n </TD> <TD>/R/n </tr>/R/n <! --/R/n <tr>/R/n <TD>/R/". getbytes (" gb2312 ");

The preceding code is used to display the page. Specifically, getbytes ("gb2312") is static encoding, which is set by resin to solve the problem of normal display in some environments. In the resin configuration file (/CONF/resin. conf, you can set the static-encoding attribute in <JSP precompile = 'true' static-encoding = 'false' recompile-on-error = 'true'/> to true or false, to control static encoding. In fact, in the resin container, all the characters are processed according to the iso-8859-1. Iso-8859-1 is a big character set, although the Chinese gb2312 and 8859 are different in the definition of the word, but the encoding is inclusive of gb2312. According to the solution of multi-language, in English-only platform with iso-8859-1 processing internal encoding, and the character display to the client machine. Therefore, as long as the encoding is correct, there is no problem in displaying Chinese on the page. Tomcat is inconvenient because Tomcat does not support Chinese characters for database operations. You need to manually hard-code the database in Java Bean or servlet. Through the test, there is no such problem in resin. Tomcat4.0 solves this problem. However, in my personal habits, I still feel that resin is easier to configure.

The imported jar can be automatically resolved in resin. This is useful when using special classes or third-party development kits. The method is very simple: just copy the jar or ZIP file to the lib directory under the resin installation directory and restart resin. For example, the db2java.zip file used by db2can be easily copied to D:/resin/lib.

Resin provides integrated debugging for JBuilder. You can go to: Workshop. Then, copy the package name: resin-jbuilder.jar to the jbuilder6/lib/EXT directory. Then, unpack resin2.1 and install it in the jbuilder6/resin-2.1 directory. Open any war project, right-click the project, select properties, and select the servers tab. In the original selection box, you can see one more resin2.1. In this way, you can debug JSP/servlet like you used tomcat, and it is more convenient than tomcat. The debugging method is the same as that of Tomcat.

V. Other problems

Resin can be used in combination with Apache. That is, Apache is used as the HTTP server, while resin is used as the srun server. See Apache. conf in the resin/CONF/Samples Directory. Set app-Dir to/usr/local/Apache/htdocs (that is, Apache Root ). At the same time, the HTTP. conf in Apache is also set accordingly. Resin also provides an automatic installation program for this process. Run resin/bin/setup. You can select Apache in the dialog box that appears. As long as you have installed Apache, resin can find the path of the httpd. conf file on its own.

Use the command line to start resin. If resin. conf is changed, resin restarts to adapt to the new configuration. This is suitable for initial installation.

Resin has good support for the database buffer pool. Here, it provides dbpool encapsulation for the buffer pool. In actual use, you only need to configure in resin. conf as follows:

<Dbpool. SQL>

<ID> orcl </ID>
<Driver> oracle. JDBC. Driver. oracledriver </driver>
<Url> jdbc: oracle: thin: @ localhost: 1521: SMTH </url>

<! -- <Url> jdbc: oracle: oci8: @ SMTH </url> -->

<User> scott </user>
<Password> tiger </password>

<Max-connections> 5 </max-connections>

</Dbpool. SQL>

Then, you can use it like this in your jsp or servlet:

Import the com. caucho. SQL. * package first, and then get the connection as follows:

Connection conn = DBPool. getPool ("ORCL"). getConnection ();

I personally recommend that you do not use the connection pool as above, or use the DataSource object directly found from the Context to obtain the connection universality according to the ejb method. The Code is also simple:

Context ctx = new InitialContext ();
DataSource ds = (DataSource) ctx. lookup ("jdbc/EmployeeDB ");
Connection conn = ds. getConnection ();

Configure jdbc in Resin as follows:

<Resource-ref>
<Res-ref-name> jdbc/EmployeeDB </res-ref-name>
<Res-type> javax. SQL. DataSource </res-type>
<Init-param driver-name = "com. caucho. jdbc. mysql. Driver"/>
<Init-param url = "jdbc: mysql_caucho: // localhost: 3306/test"/>
<Init-param user = "name"/>
<Init-param password = "password"/>
<Init-param max-connections = "20"/>
<Init-param max-idle-time = "30"/>
</Resource-ref>

Developing with Resin Web Server is still pleasant. There is no ready-made management console like Weblogic or WebSphere. But in terms of stability and convenience, Resin personally thinks it is much better than Tomcat. In addition, Resin also provides resin-cmp and resin-ejb, which are more powerful.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.