Posted on Leave a comment

IDEA community edition, jetty, war, debug, maven

Let’s create a simple java web app by IntelliJ IDEA community edition, Jetty. There is opportunity to debug the app even by IDEA CE.

Project:

2016-05-05_14-44-16

POM.XML

<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>mavenwebapp</groupId>
  <artifactId>mavenwebapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>mavenwebapp 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>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-server</artifactId>
      <version>9.3.8.v20160314</version>
    </dependency>
    <dependency>
      <groupId>org.eclipse.jetty</groupId>
      <artifactId>jetty-servlet</artifactId>
      <version>9.3.8.v20160314</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>mavenwebapp</finalName>
    <plugins>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.3.8.v20160314</version>
        <configuration>
          <!--<jettyXml>jetty-http.xml</jettyXml>-->
          <httpConnector>
            <port>8077</port>
          </httpConnector>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <welcome-file-list>
    <!--<welcome-file>index.jsp</welcome-file>-->
    <welcome-file>index.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>http_listener1</servlet-name>
    <servlet-class>http_listener1</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>http_listener1</servlet-name>
    <url-pattern>/http_listener1</url-pattern>
  </servlet-mapping>
</web-app>

http_listener1.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.jetty.http.HttpMethod;

public class http_listener1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        resp.getWriter().println("<!DOCTYPE HTML>");
        resp.getWriter().println("<html><body><p>" + "Имя пользователя: " +
                (String) req.getParameter("username") + "</p></body></html>");
    }
}

main.java

import org.eclipse.jetty.server.*;
import org.eclipse.jetty.servlet.*;
import org.eclipse.jetty.http.HttpStatus;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class main {
    public static void Main(String[] args) {
        Server sever = new Server(7075);
        ServletContextHandler handler = new ServletContextHandler(sever, "/example");
        handler.addServlet(ExampleServlet.class, "/");
//        sever.start();
    }

    public class ExampleServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            resp.setStatus(HttpStatus.OK_200);
            resp.getWriter().println("EmbeddedJetty");
        }
    }
}

index.html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Сервлет</title>
</head>
<body>

<form action="http_listener1" method="GET">
    <p>Введите имя пользователя: <input type="text" name="username"></p>
    <input type="submit" value="Отправить" />
</form>

</body>
</html>

To run or debug use popup menu items
Безымянный

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.