Showing posts with label Web. Show all posts
Showing posts with label Web. Show all posts
Sunday, March 2, 2014
Friday, February 28, 2014
Connect database using Servlet & JSP in java web project
- First, need to copy JDBC drivers into WEB-INF\lib folder. It will come up under "Web App Libraries" automatically.
- Second, create a java class for connecting to SQL Server database.
- Third, create Servlet.
- Fourth, create JSP page.
Thursday, February 27, 2014
Wednesday, February 12, 2014
Deploy eclipse web project to Tomcat
1. Export web project to WAR file.
2. Go to Tomcat Manager page, select WAR file and click "Deploy" button.
Tuesday, February 11, 2014
How to display all the session variables in an HTML page?
The session implicit object is an instance of
javax.servlet.http.HttpSession
. This variable is only valid for Http protocols. The session is one of the JSP built-in variables (like request) that is available in the service body of a JSP.
There's no way you can do it directly from plain HTML since HTML is static and the session object really "lives" in the dynamic environment of the web container. The following example JSP shows you how to generate HTML that shows the objects in the session attribute collection.
<%@ page language="java" import="java.util.*" %> <html> <body> Session attributes: <% session.setAttribute("test.name", "Test Attribute List"); session.setAttribute("test.float", new Float(5.0)); session.setAttribute("test.int", new Integer(10)); session.setAttribute("test.Object", new StringBuffer("StringBuffer")); session.setAttribute("test.boolean", new Boolean(true)); session.setAttribute("test.double", new Double(343.1)); for (Enumeration e = session.getAttributeNames(); e.hasMoreElements(); ) { String attribName = (String) e.nextElement(); Object attribValue = session.getAttribute(attribName); %> <BR><%= attribName %> - <%= attribValue %> <% } %> </body> </html>
Original link:
http://www.xyzws.com/JSPfaq/how-to-display-all-the-session-variables-in-an-html-page/33
Monday, February 10, 2014
How to use HttpSession
Returns the current session associated with this request, or if the request does not have a session, creates one.
HttpSession getSession(boolean create)
Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session.
Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session.
Sunday, February 9, 2014
Installing the BIRT Viewer in Tomcat
BIRT POJO Viewer
Starting with the release of BIRT 3.7 the Viewer has been changed to be POJO based. All required BIRT plugins are now in the WEB-INF/lib folder of the Viewer, and the WEB-INF/platform directory has been removed. The Viewer should continue to operate as in previous versions of BIRT. More information about this change is available in the BIRT 3.7 Migration Guide.
Using Tomcat
This page explains how to deploy the BIRT viewer to a Java EE container. We'll useApache Tomcat, since it is open source and readily available. The same concepts, perhaps with different details, apply to other app servers. These instructions assume you'll install Tomcat on your own machine using the default port number of 8080.
Install the Viewer
Deploy the BIRT Viewer application. Follow these steps:
- Download the zip file with the BIRT report engine runtime. The file is named birt-runtime-version#.zip.
- Unzip the file in a staging area.
- Look under the birt-runtime-
directory and locate the "Web Viewer Example" directory. - Copy the Web Viewer Example directory to the webapps directory of your Tomcat installation. For ease of reference, rename the directory to "birt-viewer".
- Stop, then restart Tomcat.
- Display the Tomcat manager application to check that the viewer is deployed:http://localhost:8080/manager/html.
- Verify that birt-viewer is listed as an application, then click on the birt-viewer link.
- A page confirming that the BIRT viewer has been installed should be displayed. Click on the link labeled "View Example" to confirm that your installation is working properly.
- The BIRT Viewer requires that cookies be enabled.
If you choose to put the Viewer into some other location, you'll need to use a context entry within the server.xml file to indicate the deployment location. See Tomcat documentation for details.
Install your JDBC Drivers
Add the jar files for your JDBC drivers to the Viewer. Copy the driver the following directory:
BIRT JDBC Driver Location Note:
- If you are installing BIRT 2 series the driver needs to be copied to birt-viewer\WEB-INF\platform\plugins\org.eclipse.birt.report.data.oda.jdbc\drivers directory.
If you are installing BIRT 3.7 or higher, the jdbc driver should be placed in the WebViewer's classpath (eg WEB-INF/lib).
Testing a More Complex Report
We'll test the viewer further using one of the example reports created for the "Classic Models" database. Note that Classic Models database is included in the birt runtime distribution so no further set-up is required. Follow these steps:
Original Link: http://www.eclipse.org/birt/phoenix/deploy/viewerSetup.php
Thursday, February 6, 2014
How to check the html button request value using JSP
Below shows you how to check the html button request value using JSP:
JSP file:
<html>
<head>
<title>E-bookshop</title>
<style type="text/css">
body {background-color:gray; font-size=10pt;}
H1 {font-size:20pt;}
table {background-color:white;}
</style>
</head>
<body>
<H1>Your online Bookshop</H1>
<hr/><p/>
<form name="addForm" method="POST">
<input type="hidden" name="do_this" value="add">
Book:
<select name=book>
<option>Learn HTML5 and JavaScript for iOS. Scott Preston $39.99</option>
<option>Java 7 for Absolute Beginners. Jay Bryant $39.99</option>
</select>
Quantity: <input type="text" name="qty" size="3" value="1">
<input type="submit" value="Add to Cart">
</form>
<p/>
<form name="checkoutForm" method="POST">
<input type="hidden" name="do_this" value="checkout">
<input type="submit" value="Checkout">
</form>
<br/>
<%=request.getParameter("do_this") %>
</body>
</html>
JSP file:
<html>
<head>
<title>E-bookshop</title>
<style type="text/css">
body {background-color:gray; font-size=10pt;}
H1 {font-size:20pt;}
table {background-color:white;}
</style>
</head>
<body>
<H1>Your online Bookshop</H1>
<hr/><p/>
<form name="addForm" method="POST">
<input type="hidden" name="do_this" value="add">
Book:
<select name=book>
<option>Learn HTML5 and JavaScript for iOS. Scott Preston $39.99</option>
<option>Java 7 for Absolute Beginners. Jay Bryant $39.99</option>
</select>
Quantity: <input type="text" name="qty" size="3" value="1">
<input type="submit" value="Add to Cart">
</form>
<p/>
<form name="checkoutForm" method="POST">
<input type="hidden" name="do_this" value="checkout">
<input type="submit" value="Checkout">
</form>
<br/>
<%=request.getParameter("do_this") %>
</body>
</html>
Tuesday, February 4, 2014
JSP basic
1. Directive
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.util.Date" %>
2. Declaration
In JSP, you can declare variables
in three ways:
<% int k = 0; %>
<%! int k = 0; %>
<%! static int k = 0; %>
The first declaration means that a new variable is created for each incoming HTTP client request;
the second one means that a new variable is created for each new instance of the servlet; and the third
one means that the variable is shared among all instances of the servlet.
Normally, Tomcat instantiates each one of these classes only once
and then creates a Java thread for each incoming request. It then executes the same servlet object within
each thread. If the application runs on a distributed environment or for high numbers of requests,
Tomcat can instantiate the same servlet more than once. Therefore, only the third declaration
guarantees that the variable will be shared among all requests.
in three ways:
<% int k = 0; %>
<%! int k = 0; %>
<%! static int k = 0; %>
The first declaration means that a new variable is created for each incoming HTTP client request;
the second one means that a new variable is created for each new instance of the servlet; and the third
one means that the variable is shared among all instances of the servlet.
Normally, Tomcat instantiates each one of these classes only once
and then creates a Java thread for each incoming request. It then executes the same servlet object within
each thread. If the application runs on a distributed environment or for high numbers of requests,
Tomcat can instantiate the same servlet more than once. Therefore, only the third declaration
guarantees that the variable will be shared among all requests.
3. Include another JSP on page
<jsp:include page="index.jsp"/>
4.Scope
There are four possible scopes. Inorder of increasing generality, they are: page, request, session, and application.
5.Using an Attribute to Enable and Disable Conditional Code
One of the advantages of using JSP is that the web server doesn’t need to reinterpret the source file of a
page every time a client requests that page. The JSP container translates each JSP page into a Java file
and compiles it into a class, but this only happens when you update the JSP source. You might like to be
able to switch on or off some particular functionality for debugging or other purposes, without having to
edit one or more file and force Tomcat to recompile them when you flip the switch. To achieve this
result, you only need to wrap the functionality in question inside a conditional statement, as the
following:
<jsp:include page="index.jsp"/>
4.Scope
There are four possible scopes. Inorder of increasing generality, they are: page, request, session, and application.
5.Using an Attribute to Enable and Disable Conditional Code
One of the advantages of using JSP is that the web server doesn’t need to reinterpret the source file of a
page every time a client requests that page. The JSP container translates each JSP page into a Java file
and compiles it into a class, but this only happens when you update the JSP source. You might like to be
able to switch on or off some particular functionality for debugging or other purposes, without having to
edit one or more file and force Tomcat to recompile them when you flip the switch. To achieve this
result, you only need to wrap the functionality in question inside a conditional statement, as the
following:
6. The out Object
The following three lines have exactly the same effect on the response:
<% out.print("abc"); %>
<%="abc"%>
abc
7.MVC
The Model 2 Architecture
A better solution, more suitable for larger applications, is to split the functionality further and use JSP
exclusively to format the HTML pages. This solution comes in the form of the JSP Model 2 architecture,
also known as the model-view-controller (MVC) design pattern (see Figure 3-2).
Includes another JSP page in a JSP page
The following code includes another JSP page in a JSP page:
...............
<jsp:include page="index.jsp"/>
...............
Monday, February 3, 2014
How to test your web project in Eclipse
Before you can test your web project, you need to define a new server. Once you define a new server, your eclipse will generate a new server project automatically:

Change Tomcat default home screen
Tomcat default home screen is located on C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\ROOT\index.jsp. In order to keep all the default functions you should copy the ROOT folder and change its name to "home", then delete everything in ROOT folder and copy your own files into this folder.
After this modification you can browse the original home screen by typing:
http://localhost:8080/home
The conf\web.xml file define what kind of file can be the start up screen.
So you can create a index.jsp, index.htm or index.html file as your startup screen.
After this modification you can browse the original home screen by typing:
http://localhost:8080/home
The conf\web.xml file define what kind of file can be the start up screen.
So you can create a index.jsp, index.htm or index.html file as your startup screen.
Saturday, February 1, 2014
How to install Tomcat in Windows 7, modify port & username & password
Step one: Download Tomcat Windows installer from Tomcat website and execute it.
Step two: Create a new rule in firewall, allow Tomcat server port to be accessible.
Step two: Create a new rule in firewall, allow Tomcat server port to be accessible.
Now you should be able to browse Tomcat server page by entering: localhost:8080 on local pc or entering xx.xx.xx.xx:8080 on other pc.
You can also change the server port by modifying the server.xml file:
You can also change the user and password by modifying the tomcat-users.xml file:
How do I give my computer a static local IP address?
Original link: http://www.boutell.com/newfaq/creating/staticlocalip.html
Repost:
2006-01-25 : If you are using a router to share your Internet connection with more than one computer in your home, or to achieve wireless Internet access (WiFi), congratulations! Your computers are protected from several common threats. That's because other computers on the Internet can't directly connect to your computers -- the router won't let them.
Repost:
Normally this is great, but when we want to host a website at home or host BitTorrent downloads at home, it's inconvenient. In these cases, we want other people to be able to connect to our computer... for those purposes only, of course.
You can skip this article if you have no router. If you have wireless networking (wifi), or more than one computer, you definitely have a router and you will definitely need to follow these steps. Occasionally a router is built right into your DSL or cable modem, in which case you will need to look at the manual for that device.
Fortunately, all well-made routers allow us to forward ports to a particular computer inside the "local" network (your collection of computers at home, "behind" the router). Unfortunately, many routers assign a new address to each computer every time it is turned on. To forward connections on web or BitTorrent ports, we first need a "static" (unchanging) local address on the local network to forward those connections to! So how do we keep our computer from getting a new address every time we power it on? That's the question I'll answer here.
A static local IP address is not the same thing as a static Internet IP address. Most cable modem and DSL ISPs give you a dynamic Internet IP address - that is, it is subject to change. That's not what I'm talking about here. I'm talking about an address on the little "private" network that exists behind your router, where your personal computers live. Unless you take the steps in this article, addresses on the local network are also subject to change, which prevents you from forwarding ports to them. That's why we set up a static local IP address for your server - so that we can tell the router which local IP address to forward web or BitTorrent traffic to.
Some routers are smart enough to let you forward ports to a computer by itsname instead of itsaddress. If so, you can skip this article. Check under "virtual server" or "port forwarding" to see if yours is that smart. Mine isn't.
There are two ways to solve this problem. If your router is well-designed, it might have a feature that lets you assign a specific, never-changing local IP address to your computer by its name on the local network. That's convenient because you don't have to change settings on the computer itself.
The second way... which is the only way with some routers, like my own SMC Barricade G and the popular Linksys WRT54G... is to assign your computer an IP address outside the range of addresses that your router gives out automatically. By forcing the computer to use a specific IP address, we avoid the problem of receiving a new address every time we power the computer on. In this case we'll make the change in your computer's network settings. But first, we'll want to double-check the range of IP addresses that your router gives out, so that two different computers never get the same IP address.
Warning: setting a static IP address for a laptop can be a pain if you want to use your laptop on someone else's network, such as in a cafe with WiFi. But if you're going to take your computer other places, your BitTorrent or web server will be out of commission for the duration anyway. So what's the bottom line? Follow these steps only on a computer that stays put and stays turned on! Your BitTorrent won't work for anyone if you take the tracker away. Find an older PC, power it up, install Windows XP or Linux on it, make sure to run Windows Update or the equivalent automatically and often, and leave that machine powered up 24/7. You've just built your first server.
I've talked about changing settings on the router. But how do we get access to your router's settings? Almost all routers include a built-in web server that provides a simple way to configure the router. And that's what we'll use. Typically you'll log in at an address like http://192.168.2.1/ (for an SMC Barricade G and many others) or http://192.168.1.1/ (for a Linksys WRT54G and many others), but see the manual for your router to be sure. Hopefully you have already configured your router with an administrative password to keep hackers from changing your settings.
I can't give you exact step-by-step instructions to do this with every router, because every router is different. Keep your router's manual handy as you read this and you'll get there!
Please don't ask me what your router password is! You chose it when you set up your router, or it is still set to the default. If you can't seem to get in, check the manual of your router for more information. There may be a default password, or the default password might be blank. In a worst-case scenario, follow your router's procedure for a hard reset (not just a power cycle -- there is usually a recessed button for this job, see the manual).
Once you have logged in to your router's configuration interface, explore the advanced settings and look for a way to assign a fixed, unchanging IP address for your computer on your local network. Your router may provide a way to do this on a page of settings devoted to DHCP (Dynamic Host Configuration Protocol). Also take note of the range of addresses automatically assigned to computers without a fixed address. Typically this is 192.168.2.100 to 192.168.2.254 or something similar.
Many routers, such as the SMC Barricade G and the Linksys WRT54G, do not have this feature. Don't worry - there's an alternative way to assign a static local IP address and I'll cover that next.
Assign your computer an address outside this range, such as 192.168.2.11 (for the Barricade) or 192.168.1.11 (for the Linksys). It must still be in the same "class C subnet." In other words, it must still begin with 192.168.2. or 192.168.1. if that is what your router's IP address begins with. Also, you may not use an address ending in .255, which is reserved for special "broadcast" messages.
Now reboot your computer. At the Windows command line (Start -> Run -> cmd.exe), the ipconfig command should now report the fixed IP address that you assigned.
If Your Router Doesn't Let You Set Static Local IPs
If your router doesn't allow you to assign fixed local IPs in this way, all is not lost. You can still assign a fixed IP via the Windows Control Panel (or the Macintosh or Linux equivalent). Follow these steps to force your computer to use a fixed IP address:
This is tricky stuff and you can mess up your connection if you do it wrong. So read carefully. If you don't understand something, don't just try things at random... read more instead!
1. Choose an address. It must be in the same "class C subnet" as your router. That is, if your router's web interface is at http://192.168.2.1/ (typical for the SMC Barricade G) or http://192.168.1.1/(typical for the Linksys WRT54G), then you can choose an address where the last of those four numbers is different. The number you choose must be between 2 and 254, and you must choose an address that won't be dynamically assigned to other computers. The DHCP settings page of your router's web interface will tell you what range of addresses are dynamically assigned by DHCP. Many routers, including both the SMC Barricade G and the Linksys WRT54G, are set up "out of the box" not to assign addresses ending in a number between .2 and .99. So anything in that range is a good choice for your server. For instance, with my SMB Barricade G, 192.168.2.11 is a good choice for a static local IP. With the Linksys WRT54G, 192.168.1.11 is a good choice.
2. Access the Network Connections control panel of Windows XP (Start -> Control Panel -> Network Connections). Pick the connection you're using, either Local Area Connection (for wired Ethernet cables) or Wireless Network Connection (for WiFi). Right-click on that connection's icon and pick Properties. Under "this connection uses the following items," scroll down to "Internet Protocol (TCP/IP)" and double-click on that. The "Internet Protocol (TCP/IP) Properties" window will appear.
3. Right now, "Obtain an IP address automatically" is probably selected. Instead, select "Use the following IP address." In the "IP address:" field, enter the address you chose (for example, 192.168.2.11 works well with my SMC Barricade G, while 192.168.1.11would work well with a Linksys WRT54G). The subnet mask will automatically become 255.255.255.0, which is correct. Set the "Default gateway" field to the address of your router, which is usually 192.168.2.1 (for the Barricade G) or 192.168.1.1 (for the Linksys WRT54G) -- but please don't copy and paste this! Use the address that actually gives you access to the router's web interface.
4. "Use the following DNS server addresses:" is now selected. In the "Preferred DNS server:" field, enter the IP address of your router (the same as the default gateway). Leave "Alternate DNS server:" blank.
5. Click "OK" to leave the "Internet Protocol (TCP/IP) Properties" window, and again to leave the connection properties window. In Windows XP, your IP address will change immediately at this point. Verify that you can still access the Internet. If not, double-check your work.
If you mess up and you're not sure why, you can restore your normal settings by returning to the "Internet Protocol (TCP/IP) Properties" window and re-selecting "Obtain an IP address automatically" and "Obtain a DNS server address automatically."
OK, great, your computer now has a fixed address on the local network. What does that do for us? It gives us a destination for incoming web or BitTorrent traffic. All we have to do now is tell the router what to do with inbound traffic on the ports we've chosen for BitTorrent. For most readers, the next step is to forward ports through the router."My server doesn't show up on the client list in my router!"
The "client list" displayed by some routers is just a list of PCs that were assigned addresses dynamically by the built-in DHCP server in the router. You have a static IP address for your server, so it doesn't show up. This is normal, and it's a good thing.Friday, January 31, 2014
Install Tomcat on Mac OS X
Install Tomcat on Mac OS X
The most simple way is:
Other way from internet is as below:
(Original link: https://code.google.com/p/gbif-providertoolkit/wiki/TomcatInstallationMacOSX)
- Download Tomcat .zip file from internet, unzip it and then rename it to "Tomcat".
- Copy "Tomcat" folder to /Library or other folder.
- run terminal, execute the following commands:
cd /Library/Tomcat/bin
chmod +x startup.sh
chmod +x shutdown.sh
chmod +x catalina.sh
chmod +x setclasspath.sh
chmod +x bootstrap.jar
chmod +x tomcat-jni.jar - Run Tomcat by enter the below command:
./startup.sh
Other way from internet is as below:
(Original link: https://code.google.com/p/gbif-providertoolkit/wiki/TomcatInstallationMacOSX)
Table of Contents
- Install Tomcat on Mac OS X
- Table of Contents
- Prerequisites
- Steps
- 1) Download Tomcat 6.x
- 2) Install Tomcat 6.x
- 3) Edit the Tomcat Configuration
- 4) Run Tomcat
- 5) Test Tomcat
- 6) Shut down Tomcat
- 7) Running Tomcat as a service daemon
- 8) References
This tutorial explains how to install the Apache Tomcat 6.0.x on Mac OS X 10.5 or 10.6. This document is not tested to work with other versions of Tomcat or Java. For complete details, please consult the specific documentation for those software installations.
This tutorial is for now 'partially' tested with Tomcat 7.
Prerequisites
Following are the conditions assumed to be true in order to follow this tutorial.
1) The client version of OS X 10.5.8 Leopard or OS X 10.6 Snow Leopard. If you're running Server version of Mac OS X 10.5 or 10.6, Tomcat is pre-installed.
2) The latest security upgrades.
3) JAVA 5 or JAVA 6 Framework installed.
4) Logged in as an administrator.
Steps
1) Download Tomcat 6.x
Download the latest stable Tomcat 6 Binary Distribution Core (tar.gz) release from http://tomcat.apache.org/download-60.cgi. This should put a file of the form apache-tomcat-6.x.x.tar.gz (or apache-tomcat-6.x.x.tar if you download with Safari) into your Downloads folder.
2) Install Tomcat 6.x
Open the Terminal application to get a command prompt. The commands that follow assume that the Bourne Again SHell (bash) is in use. You can find out which shell you are using by typing the following and then hitting the ENTER key in the Terminal's command prompt:
echo $SHELL
All versions of OS X later than 10.3 use bash as the default shell. If the result echo command does not end in "/bash", you can change the default shell by using the System Preferences Accounts pane. If the pane is locked, unlock it. Control Click on your account name and a contextual menu will appear. Click on Advanced Options. You will then be presented with a dialog where you can change the default login shell to whatever you want. Select "bin/bash".
Change into the Library directory:
cd /Library
Create the Tomcat directory:
mkdir Tomcat
Set the owner of the Tomcat directory, where username should be the login name under which Tomcat will run:
chown username Tomcat
Set the group for the Tomcat directory to admin:
chgrp admin Tomcat
Change into the newly created Tomcat directory:
cd Tomcat
If the downloaded file was not already unzipped, unpack and unzip it into the Tomcat directory:
tar -xvzf ~/Downloads/apache-tomcat-6.x.x.tar.gz
Otherwise unpack the tar file:
tar -xvf ~/Downloads/apache-tomcat-6.0.x.tar
Create a Home symbolic link that points to the Tomcat directory:
ln -sfhv apache-tomcat-6.x.x Home
3) Edit the Tomcat Configuration
You will need to add a name and password to the tomcat-users.xml configuration file to access the Tomcat management and administration programs. Execute the following commands in Terminal:
Change into the Tomcat configuration directory:
cd Home/conf
Edit the tomcat-users.xml file. This example shows the command to edit using nano:
nano tomcat-users.xml
In the file, add the two lines below into the file above the line that says </tomcat-users> and outside of any comments (delimited with <!-- and-->). Substitute the name you want as the admin's username for "admin" and enter a password for that user to log in to the Tomcat Manager in place of "password".
<role rolename="manager"/> <user username="admin" password="password" roles="standard,manager,admin"/>
If you're setting up Tomcat 7, the role is defined differently:
<user username="admin" password="password" roles="manager-gui"/>
In Tomcat 7, role names are automatically created.
Save the tomcat-users.xml file and exit from the editor.
4) Run Tomcat
Execute the following commands in Terminal: Change into the directory where Tomcat startup scripts are located
cd ../bin
Remove all of the scripts ending with .bat.
rm *.bat
Execute the Tomcat startup script:
./startup.sh
Check the Tomcat error log for errors:
less ../logs/catalina.out
If there are no error messages in the log files, then the installation has been completed successfully and Tomcat is running. There should be an informational message similar to the following near the end of the log file:
INFO: Server startup in 2412 ms
Under some circumstances the startup scripts do not execute because the execute permission has not been set. If this is the case you can change the execute permission to the scripts by typing the following:
cd ../bin chmod 750 *.sh
This signifies read, write, and execute permissions for the owner, read and execute permissions for the group, and no permissions for others.
5) Test Tomcat
If Tomcat is running successfully following step 4, above, you should be able to see the Tomcat Welcome page at the following URL:
6) Shut down Tomcat
To shut down Tomcat type the following from the ./bin directory where Tomcat was installed (/Library/Tomcat/Home following the steps on this page):
./shutdown.sh
7) Running Tomcat as a service daemon
Mac OS X introduced launchd as the system-wide service management framework when Mac OS X 10.4 Tiger was released. Since then lanuchd succeeded traditional cron job management as the preferred way of daemonise system services on Mac OS X.
With the previous setup, to start up Tomcat while booting:
1. Create a script as [Tomcat home]/bin/tomcat-launchd.sh to start Tomcat as a non-daemonised process:
#!/bin/sh # # Wrapper for running Tomcat under launchd # Required because launchd needs a non-daemonizing process function shutdown() { $CATALINA_HOME/bin/shutdown.sh /bin/rm $CATALINA_PID} function wait_for_death() { while /bin/kill -0 $1 2> /dev/null ; do sleep 2 done } export CATALINA_PID=$CATALINA_HOME/logs/tomcat.pid $CATALINA_HOME/bin/startup.sh trap shutdown QUIT ABRT KILL ALRM TERM TSTP sleep 2 wait_for_death `cat $CATALINA_PID`
2. Create a launchd plist at /Library/LaunchDaemons/org.apache.tomcat.plist
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>org.apache.tomcat</string> <key>Disabled</key> <false/> <key>OnDemand</key> <false/> <key>RunAtLoad</key> <true/> <key>ProgramArguments</key> <array> <string>/Library/Tomcat/bin/tomcat-launchd.sh</string> </array> <key>EnvironmentVariables</key> <dict> <key>CATALINA_HOME</key> <string>/Library/Tomcat</string> <key>JAVA_OPTS</key> <string>-Djava.awt.headless=true</string> </dict> <key>StandardErrorPath</key> <string>/Library/Tomcat/logs/tomcat-launchd.stderr</string> <key>StandardOutPath</key> <string>/Library/Tomcat/logs/tomcat-launchd.stdout</string> <key>UserName</key> <string>_appserver</string> </dict> </plist>
3. Load the launchd process by
$ sudo launchctl load /Library/LaunchDaemons/org.apache.tomcat.plist
You can replace the load subcommand as unload, stop, start to remove Tomcat from startup processes, stop or start it, respectively.
Please also note that, the sample launchd plist assume the service will run as the user _appserver, which is predefined on Mac OS X for running app services. So you'll have to make sure the Tomcat directories and IPT data directories are owned, writable and executable by_appserver. Or, please refer to this page.
8) References
For more thorough descriptions of Tomcat configuration on Mac OS X, also consult these sources:
- Tomcat documentation: http://tomcat.apache.org/tomcat-6.0-doc/index.html
- Tomcat wiki: http://wiki.apache.org/tomcat/TomcatOnMacOS
- launchd intro: http://en.wikipedia.org/wiki/Launchd
- launchd wrapper: http://confluence.atlassian.com/display/DOC/Start+Confluence+automatically+on+OS+X+using+launchd
Subscribe to:
Posts (Atom)