I started learning Java Servlets because many people suggested me to start with Servlets before moving to Spring and other JavaEE frameworks and technologies. Tutorialspoint has many examples, but I can't run them on anything other than Tomcat. I am using Netbeans IDE, Maven and Glassfish. How could I run a basic POST example with these software packages? Link to the example:
-
You haven't told us how you're trying to deploy your application on Glassfish. Are you getting any exceptions? – ujulu Jul 29 '17 at 11:45
-
1Please read vendor's own official tutorials (thus, at oracle.com website) and authoritative books (thus, the real paper ones you can buy at amazon.com) instead of a random tutorial site hosted in India full of disturbing advertising banners maintained by amateurs with the primary focus on SEO abuse and earning money rather than genuinely helping out other developers. – BalusC Jul 29 '17 at 12:42
-
Thanks for suggestions. I intended to answer this question myself, based on my own research (including documents and tutorials from oracle.com). It took me a bit of time to figure this out, and I thought other beginners might benefit from my answer. – Chase Jul 29 '17 at 13:00
1 Answers
1. Setting up pom.xml:
In Netbeans IDE create a new maven project. Change the packaging to war in pom.xml.
<packaging>war</packaging>
Add this dependency to dependencies in pom.xml. This will add the necessary servlet classes to classpath.
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
Set up maven-war-plugin version. With version 3.0.0 or higher you don't have to create ServletRoot/WEB-INF/web.xml. If you skip this step, the project won't compile without configuring web.xml. I called "ServletRoot" this path: mavenproject/src/main/webapp A good explanation about package directory structure can be found here at 3.1: package structure
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
2. Put index.html Create index.html into mavenproject/src/main/webapp and copy this codesnippet there. Take note that the form tag's action attribute's value must match your servlet's WebServlet annotation's value. In this case it will be this:
@WebServlet("/postexample")
index.html:
<html>
<body>
<form action = "postexample" method = "POST">
First Name: <input type = "text" name = "first_name">
<br />
Last Name: <input type = "text" name = "last_name" />
<input type = "submit" value = "Submit" />
</form>
</body>
</html>
3. Create a new class in your package. Don't use default package.
I named it POSTExample. Here is the code without package declaration:
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
// Extend HttpServlet class
@WebServlet("/postexample")
public class POSTExample extends HttpServlet {
// Method to handle GET method request.
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Using GET Method to Read Form Data";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n" +
"<ul>\n" +
" <li><b>First Name</b>: "
+ request.getParameter("first_name") + "\n" +
" <li><b>Last Name</b>: "
+ request.getParameter("last_name") + "\n" +
"</ul>\n" +
"</body>\n" +
"</html>"
);
}
// Method to handle POST method request.
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
4. Build maven project In Netbeans IDE you have a button for it. Neteans can also deploy it automatically to glassfish, but I haven't tried that. So I will show you how to deploy it manually with glassfish manager. You can build it in commandline with.
mvn clean package
5. Deploy Open commandline and use this command. It will start glassfish server, and will log a lot.
asadmin start-domain --verbose
Open a browser and enter this URI. I'm assuming you are using the default settings for glassfish. Default Ports, etc...
Under "Common Tasks" you can find "Applications". Click on it. Then click Deploy. It's above the listed apps if there are any. Now you must enter the location of your created war file. Or you can drag n' drop. It's location is mavenproject/target/yourwarfile.war
Before clicking OK in the glassfish manager, give a simple a text for "context root" like "myservlet". You must remember this. If you did everything well, you can run the example in your browser by using this link: http://localhost:8080/myservlet
In this case "myservlet" is the context root you set in glassfish manager.

- 106
- 1
- 9