Skip to content

Commit 5f88960

Browse files
committed
Make a servlet project.
1 parent 5b60d37 commit 5f88960

File tree

10 files changed

+123
-195
lines changed

10 files changed

+123
-195
lines changed

build.xml

Lines changed: 17 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?xml version="1.0"?>
2-
<project name="SampleJavaProject" default="jar"
2+
<project name="SampleServletProject" default="war"
33
xmlns:ivy="antlib:org.apache.ivy.ant">
44

55
<!-- Project-specific configuration -->
66
<loadproperties srcfile="project.properties"/>
7-
<property name="main.class" value="${package.main}.${ant.project.name}"/>
87
<property name="base.name" value="${artifactId}-${version}"/>
98

109
<!-- Standard Directory Layout -->
1110
<property name="src.dir" value="src"/>
11+
<property name="webapp.dir" value="webapp"/>
1212
<property name="test.dir" value="test"/>
1313
<property name="build.dir" value="build"/>
1414
<property name="dist.dir" value="dist"/>
@@ -35,50 +35,23 @@
3535
</copy>
3636
</target>
3737

38-
<target name="jar" depends="compile"
39-
description="Generate the jarfile distributable.">
40-
<jar destfile="${dist.dir}/${base.name}.jar"
41-
basedir="${build.dir}/classes">
42-
<manifest>
43-
<attribute name="Main-Class" value="${main.class}"/>
44-
</manifest>
45-
</jar>
46-
<jar destfile="${dist.dir}/${base.name}-all.jar"
47-
basedir="${build.dir}/classes">
48-
<zipgroupfileset refid="runtime.fileset"/>
49-
<manifest>
50-
<attribute name="Main-Class" value="${main.class}"/>
51-
</manifest>
52-
</jar>
38+
<target name="war" depends="compile"
39+
description="Generate the warfile distributable.">
40+
<war destfile="${dist.dir}/${base.name}.war"
41+
basedir="${webapp.dir}" webxml="web.xml">
42+
<lib refid="runtime.fileset"/>
43+
<classes dir="${build.dir}/classes"/>
44+
</war>
5345
</target>
5446

55-
<target name="run" depends="compile" description="Run the application.">
56-
<java classname="${main.class}" classpath="${build.dir}/classes">
57-
<classpath refid="build.classpath"/>
58-
<arg value="--name"/>
59-
<arg value="Java"/>
60-
</java>
61-
</target>
62-
63-
<target name="run-hotswap" depends="compile"
64-
description="Run the application in hotswap mode.">
65-
<java classname="${main.class}" classpath="${build.dir}/classes"
66-
fork="true">
67-
<classpath refid="build.classpath"/>
68-
<jvmarg line="-Xdebug -Xrunjdwp:transport=dt_socket,address=9000,server=y,suspend=n"/>
69-
<arg value="--loop"/>
70-
<arg value="--name"/>
71-
<arg value="Java"/>
72-
</java>
73-
</target>
74-
75-
<target name="hotswap" depends="compile">
76-
<taskdef name="hotswap" classname="dak.ant.taskdefs.Hotswap"/>
77-
<hotswap verbose="true" port="9000">
78-
<fileset dir="${build.dir}/classes" includes="**/*.class">
79-
<modified/>
80-
</fileset>
81-
</hotswap>
47+
<target name="run" depends="war" description="Run the webapp.">
48+
<taskdef classpathref="test.classpath" resource="tasks.properties"
49+
loaderref="jetty.loader"/>
50+
<property name="temp.dir" value="${java.io.tmpdir}/${user.name}-jetty"/>
51+
<jetty tempDirectory="${temp.dir}">
52+
<webapp contextpath="/" warfile="${dist.dir}/${base.name}.war"/>
53+
</jetty>
54+
<delete dir="${temp.dir}"/>
8255
</target>
8356

8457
<target name="clean" description="Delete all generated files.">
@@ -160,30 +133,4 @@
160133
</batchtest>
161134
</junit>
162135
</target>
163-
164-
<!-- Maven repository support -->
165-
166-
<target name="bundle" depends="jar,javadoc"
167-
description="Stage a Maven repository bundle.">
168-
<!-- Pack up the artifacts -->
169-
<filter filtersfile="project.properties"/>
170-
<filter token="ant.project.name" value="${ant.project.name}"/>
171-
<copy file="pom.xml" filtering="true"
172-
tofile="${dist.dir}/bundle/${base.name}.pom"/>
173-
<copy file="${dist.dir}/${base.name}.jar" todir="${dist.dir}/bundle/"/>
174-
<jar jarfile="${dist.dir}/bundle/${base.name}-javadoc.jar">
175-
<fileset dir="${dist.dir}/javadoc"/>
176-
</jar>
177-
<jar jarfile="${dist.dir}/bundle/${base.name}-sources.jar">
178-
<fileset dir="${src.dir}"/>
179-
</jar>
180-
<!-- Sign all the things. You'll need gpg-agent help here. -->
181-
<apply executable="gpg">
182-
<arg value="--detach-sign"/>
183-
<arg value="--armor"/>
184-
<fileset dir="${dist.dir}/bundle"/>
185-
</apply>
186-
<!-- Pack it all up -->
187-
<jar destfile="${dist.dir}/bundle.jar" basedir="${dist.dir}/bundle"/>
188-
</target>
189136
</project>

ivy.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
<conf name="test" extends="build" visibility="private"/>
88
</configurations>
99
<dependencies>
10-
<dependency org="commons-cli" name="commons-cli" rev="1.2"
11-
conf="default"/>
12-
<dependency org="junit" name="junit" rev="4.10"
13-
conf="test->default"/>
14-
<dependency org="org.projectlombok" name="lombok" rev="0.10.4"
10+
<dependency org="org.mortbay.jetty" name="servlet-api" rev="3.0.20100224"
1511
conf="build->default"/>
12+
<dependency org="org.mortbay.jetty" name="jetty-ant" rev="7.4.2.v20110526"
13+
conf="test->default"/>
14+
<dependency org="org.apache.tomcat" name="jasper" rev="6.0.29"
15+
conf="test->default"/>
1616
<dependency org="com.puppycrawl.tools" name="checkstyle" rev="5.5"
1717
conf="build->default"/>
1818
</dependencies>

project.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Project information
2-
artifactId = sample-java-project
2+
artifactId = sample-servlet-project
33
description = An Ant-based sample Java project.
44
version = 1.0-SNAPSHOT
5-
package.main = sample.java.project
5+
package.main = sample
66

77
# External information
88
groupId = com.nullprogram

src/sample/Login.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package sample;
2+
3+
import java.io.PrintWriter;
4+
import java.io.IOException;
5+
6+
import javax.servlet.ServletException;
7+
import javax.servlet.http.HttpSession;
8+
import javax.servlet.http.HttpServlet;
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
12+
public class Login extends HttpServlet {
13+
private static final long serialVersionUID = 1015854638037386371l;
14+
public static final int TIMEOUT = 5 * 60; // seconds
15+
16+
@Override
17+
public void doPost(HttpServletRequest req, HttpServletResponse res)
18+
throws ServletException, IOException {
19+
if ("/logout".equals(req.getServletPath())) {
20+
req.getSession().invalidate();
21+
} else {
22+
String name = req.getParameter("name").replaceAll("[^\\w]", "");
23+
if (name.length() > 0) {
24+
HttpSession session = req.getSession();
25+
session.setAttribute("name", name);
26+
/* Automatical "logout". */
27+
session.setMaxInactiveInterval(TIMEOUT);
28+
}
29+
}
30+
res.sendRedirect("count.jsp");
31+
}
32+
}

src/sample/java/project/SampleJavaProject.java

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/sample/java/project/package-info.java

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/sample/java/project/SampleJavaProjectTest.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

web.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http:/java.sun.com/dtd/web-app_2_3.dtd">
4+
<servlet>
5+
<servlet-name>login</servlet-name>
6+
<servlet-class>sample.Login</servlet-class>
7+
</servlet>
8+
9+
<servlet-mapping>
10+
<servlet-name>login</servlet-name>
11+
<url-pattern>/login</url-pattern>
12+
<url-pattern>/logout</url-pattern>
13+
</servlet-mapping>
14+
</web-app>

webapp/count.jsp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<html>
2+
<head>
3+
<title>JSP Demo</title>
4+
</head>
5+
<body>
6+
<h1>JSP Demo</h1>
7+
<%!
8+
private int counter;
9+
10+
private synchronized int count() {
11+
return ++counter;
12+
}
13+
%>
14+
<%
15+
String name = (String) session.getAttribute("name");
16+
if (name != null) { %>
17+
<p>Welcome back, <%= name %>.</p>
18+
<form action="/logout" method="post">
19+
<input type="submit" value="logout"/>
20+
</form>
21+
<%
22+
} else { %>
23+
<form action="login" method="post">
24+
<input type="text" name="name"/>
25+
<input type="submit" value="login"/>
26+
</form>
27+
<%
28+
}
29+
%>
30+
<p>
31+
This page has been visited <%= count() %> times.
32+
</p>
33+
</body>
34+
</html>

webapp/index.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<html>
2+
<head>
3+
<title>Sample HttpServlet</title>
4+
</head>
5+
<body>
6+
<h1>Sample HttpServlet</h1>
7+
<p>
8+
Looks like everything's working fine so far, but this is just a
9+
static page.
10+
</p>
11+
<form action="login" method="post">
12+
<input type="text" name="name"/>
13+
<input type="submit" value="login"/>
14+
</form>
15+
<ul>
16+
<li><a href="count.jsp">JSP Counter</a></li>
17+
</ul>
18+
</body>
19+
</html>

0 commit comments

Comments
 (0)