Google

Apr 26, 2014

Creating a simple Java Web project with Maven tutorial -- Part 3 with eclipse

The part-1 and part-2 built the simpleWeb war  file outside eclipse using Maven. In this post, let's cover how eclipse interacts with Maven.

Step 1: start up your eclipse and verify if it has m2e (Maven to Eclipseplugin. Click on Help --> About Eclipse


Step 2:  Eclipse needs to know where your Maven settings.xml file is so that it can access your local repository.  Click on Window --> Preferences --> Maven --> User Settings.



Step 3: You can import any Maven artifact into eclipse. That is with a pom.xml file.  Click on File --> Import, and select "Existing Maven Projects" from within Maven.

Step 4: Browse and select your simpleWeb project.


Step 5: You can now work within eclipse.


Step 6:  Go to your DOS command prompt, and then run mvn eclipse:eclipse from the simpleWeb folder.

Step 7: Now come back to eclipse and select simpleWeb and press F5 to refresh the project. You should now see the referenced libraries read from your pom.xml file.



Step 8:  You can now run within eclipse by right-clicking on simpleWeb and then selecting RunAs  and then Maven clean, Maven install, Maven  test, etc. Alternatively, select "Maven build" and eneter a goal to execute like "package".




Labels: ,

Mar 6, 2014

Creating a simple Java Web project with Maven tutorial -- Part 2 Understanding war and pom.xml

Creating a simple Java Web project with Maven tutorial -- Part 1 covered developing a simple Java web application using Maven. This part is all about understanding the war and pom files.

1. The lib directory


Q. How did the servlet-api-2.5.jar get into the WEB-INF/lib folder?
A. You need to understand the pom.xml file that we used n part-1 tutorial.


There are 6 scopes, and most popular ones are

  • compile:  this is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.
  • provided: this is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
  • runtime: this scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
  • test: this scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases.

Q. Where do the compiled class files and web.xml need to be in the war file?
A. WEB-INF/classes and WEB-INF respectively.



Q. Where do web resources like .jsp, .html, .css, .jpg, .gif, .jsm etc go?
A. If you want it to be publicly accessible via a url like http://localhost:7002/simpleWeb/index.jsp then you need to put in the root (i.e. simpleWeb.war). If you want to protect its direct access, then in subfolders inside WEB-INF. Create folders for html, images, JavaScrip, css, etc. The protected approach is recommended.


Q. What if you want the war file to published to your local maven repository?
A. Run the "mvn install" command.

After running the command, inspect your local maven repository



Q. Where did maven get the group, artifiactId, and version info?
A. From the pom.xml file.


Labels: ,

Mar 4, 2014

Creating a simple Java Web project with Maven tutorial -- Part 1

Three  part tutorial for Java beginners to create a simple web application (i.e. a Web ARchive file -- i.e. a war file).
  1. Part-1: Without using an IDE like eclipse. Just using Maven, Java, and text editor like Notepad++.
  2. Part-2: Analyzing the war file and Maven pom.xml file.
  3. Part-3Import the project into eclipse and use m2e plugin for Maven within eclipse.
 Step 1: Execute the Maven maven-archetype-webapp command to create the Maven web structure with the following command on a DOS command prompt. This assumes that you have set up Java and Maven.


mvn archetype:generate -DgroupId=com.mytutorial -DartifactId=simpleWeb  -DarchetypeArtifactId=maven-archetype-webapp


Enter "0.1-SNAPSHOT" when prompted for the version and "Y" to package: com.mytutorial.

Step 2: You can verify the structure created in the file system as shown below. pom.xml is the maven file to define build details and web.xml is web deployment descriptor file.




Step 3: open the pom.xml file in a text editor like Notepad++. If you don't already have it, download it. Add servlet-api as a dependency.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mytutorial</groupId>
  <artifactId>simpleWeb</artifactId>
  <packaging>war</packaging>
  <version>0.1-SNAPSHOT</version>
  <name>simpleWeb Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 
 <dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
                <version>2.5</version>
 </dependency>
 
  </dependencies>
  <build>
    <finalName>simpleWeb</finalName>
  </build>
</project>


Step 4: Define a simple servlet class in a package com.mytutorial. You need to first create a folder named "java" under src/main. Then create folders com and com/mytutorial under the folder java. You can use the notepad++

Create a Java class named  SimpleServlet.java

package com.mytutorial;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



public class SimpleServlet extends HttpServlet {

 private static final long serialVersionUID = 1L;

 @Override
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
  PrintWriter out = response.getWriter();
  out.println("<html>");
  out.println("<body>");
  out.println("<h1>Simple</h1>");
  out.println("</body>");
  out.println("</html>");
 }

}

Step 5: Go to the DOS command prompt and change directory to simpleWeb where the pom.xml file is. type the command mvn compile. This should compile your SimpleServlet.java.

C:\Users\akumaras\projects\simpleWeb>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building simpleWeb Maven Webapp 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ simpleWeb ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ simpleWeb ---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\Users\akumaras\projects\simpleWeb\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.399s
[INFO] Finished at: Wed Feb 26 17:29:02 EST 2014
[INFO] Final Memory: 7M/243M

Note that in the file system SimpleServlet.class file gets created under target/classes/com/mytutorial.

Step 6: You need to define the web description in the web.xml descriptor file.  You need to configure your Servlet details here.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

 <display-name>Simple web app</display-name>

 
 <servlet>
  <servlet-name>Simple Servlet</servlet-name>
  <servlet-class>com.mytutorial.SimpleServlet</servlet-class>
 </servlet>

 <servlet-mapping>
  <servlet-name>Simple Servlet</servlet-name>
  <url-pattern>/simple</url-pattern>
 </servlet-mapping>


</web-app>


Step 7: Java web applications are packaged as a war file. In order to package it, you need to run the Maven command mvn package.

C:\Users\akumaras\projects\simpleWeb>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building simpleWeb Maven Webapp 0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ simpleWeb ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ simpleWeb ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ simpleWeb ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\Users\akumaras\projects\simpleWeb\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ simpleWeb ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ simpleWeb ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:2.2:war (default-war) @ simpleWeb ---
[INFO] Packaging webapp
[INFO] Assembling webapp [simpleWeb] in [C:\Users\akumaras\projects\simpleWeb\target\simpleWeb]
[INFO] Processing war project
[INFO] Copying webapp resources [C:\Users\akumaras\projects\simpleWeb\src\main\webapp]
[INFO] Webapp assembled in [53 msecs]
[INFO] Building war: C:\Users\akumaras\projects\simpleWeb\target\simpleWeb.war
[INFO] WEB-INF\web.xml already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.575s
[INFO] Finished at: Wed Feb 26 17:45:45 EST 2014
[INFO] Final Memory: 6M/243M
[INFO] ------------------------------------------------------------------------

Inspect your file system for the war file simpleWeb.war under target. If you are curious to see what files are inside, you can unzip it with a WinZip utility.


Step: This war file can be deployed to any web container or application server. For example, Tomcat, JBoss, Weblogic, etc. I deployed this to my local Weblogic server running on port 7002.

http://localhost:7002/simpleWeb/simple


Where "simpleWeb" is the name of the war file you had created, and "simple" is the "url-pattern" you defined in your web.xml file.



A simple Java web tutorial without any IDE like eclipse. This also gives you an idea as to how Maven works.

Labels: ,

Sep 25, 2012

Java tutorial: Create a simple Web app using JEE

Install Apache Tomcat application server

To run JEE based web apps, you need an  application server that contains a web container. There are a number of application servers like Apache Tomcat, JBoss, GlassFish, etc. This tutorial will use Apache Tomcat.

Step 1: Download and install Apache Tomcat from http://tomcat.apache.org/download-70.cgi. The installing is all about unzipping it to the right folder. If you are running on Windows 7, download the 64-bit windows zip.




Continue with creating a Web project.

Step 2: Generate a Web project structure using Maven as we did for simple Java project in the previous tutorial. Run the command under C:\Temp\Java\projects.

mvn archetype:generate -DgroupId=com.mytutorial -DartifactId=simpleWeb  -DarchetypeArtifactId=maven-archetype-webapp

This would have created  the simpleWeb and a number of other sub folders adhering to the maven convention.


As you can see, it had generated a webapp folder and the web.xml.

Step 3: Import this project into eclipse. This step is very similar to the simple Java tutorial.


Also, note that in the top right hand corner  the tab says that eclipse is in Java EE perspective.

Integrate Tomcat server into eclipse so that you can it within eclipse.

Step 4: From eclipse top menu select "Window --> Preferences"





Step 5: Select the path to previously installed Tomcat server.




Step 6: Add this new server to the panel marked "Server". This where the "Console" tab is also located. This will also allow you to deploy the "simpleWeb" project to the Tomcat  server.





Step 7: Deploy the simpleWeb project by selecting it from the left pane marked "Available" and adding to the right pane marked "Configured".



Step 8: Start the Tomcat server that was deployed with the simpleWeb within eclipse by right-mouse-clicking on the server and selecting "Start".




Step 9: The console tab will output the server logs.



Step 10: Once the server has started, you type the following URL in a browser of your choice. Now you have a very simple Web application running in Tomcat server.

http://localhost:8080/simpleWeb/index.jsp



Now, let's see how we can build the war  package and deploy it to Tomcat from outside eclipse.

Step 11: Firstly, use Maven to build the war package. These steps are very similar to simple Java tutorial, except for deploying the Web ARchive (i.e. the war file) to Tomcat, and starting Tomcat outside eclipse. If your Tomcat is already running within eclipse stop it by selecting the server and then right-mouse-clicking to bring up the context menu, and then selecting the "Stop" button. You can check the "console" tab to see if the server has stopped.


Step 12: Now, use the "mvn" command to build the war file after changing the directory to "simpleWeb"  in a command line.  You need to be in the "simpleWeb" folder because that is where the pom.xml file is.


c:\Temp\Java\projects\simpleWeb>mvn clean install

Step 13: You can see the generate war file deployed to your local maven repository.



Step 14: To deploy the simpleWeb-1.0-SNAPSHOT.war file to Tomcat, copy it to the Tomcat's "webapps" folder, and then you can start the server by double clicking on "startup.bat", which will deploy the application when the server starts up. It simply unzips the simpleWeb-1.0-SNAPSHOT.war file by creating a new folder "simpleWeb-1.0-SNAPSHOT", which will have all the required files.





Step 15: Once the server is started, you can access the application in browser of your choice with the following URL.

http://localhost:8080/simpleWeb-1.0-SNAPSHOT/index.jsp


As you can notice, "simpleWeb-1.0-SNAPSHOT" is the exploded folder under the "webapps" folder within Tomcat file structure.

You may also like

Labels: , ,