{"id":509,"date":"2015-03-29T16:48:00","date_gmt":"2015-03-29T11:18:00","guid":{"rendered":"http:\/\/techtwaddle.net\/?p=509"},"modified":"2019-02-01T13:16:29","modified_gmt":"2019-02-01T07:46:29","slug":"creating-a-simple-java-web-app-using-intellij-idea-and-setting-up-remote-debugging","status":"publish","type":"post","link":"https:\/\/techtwaddle.co.in\/blog\/2015\/03\/29\/creating-a-simple-java-web-app-using-intellij-idea-and-setting-up-remote-debugging\/","title":{"rendered":"Creating a simple java web app using IntelliJ IDEA and setting up remote debugging"},"content":{"rendered":"<p>I had to get this setup up and running at work, thought it\u2019ll be a good idea to jot it down here. The first step is to install IntelliJ IDE from <a href=\"https:\/\/www.jetbrains.com\/idea\/download\/\">here<\/a>. I installed the ultimate edition which has a free 30-day trial, <del>but the steps below should work well with the free community edition as well<\/del> (looks like this does not work on community edition). We\u2019ll be hosting the app on Tomcat server (running on a remote machine) so go ahead and install it from <a href=\"http:\/\/tomcat.apache.org\/download-80.cgi\">here<\/a>. I installed version 8 using the windows service installer. And of course, since you\u2019re developing a java app make sure you have the <a href=\"http:\/\/www.oracle.com\/technetwork\/java\/javase\/downloads\/jdk8-downloads-2133151.html\">jdk installed<\/a>.<\/p>\n<p>Launch IDEA and create a new project, we\u2019ll call it <em>SimpleJavaWebApp<\/em>. Select <em>Java Enterprise<\/em> and <em>Web Application<\/em>. Make sure the project SDK is set correctly and application server is set to the version of Tomcat you installed.<\/p>\n<p><a href=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image.png\"><img decoding=\"async\" loading=\"lazy\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" src=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image_thumb.png\" alt=\"image\" width=\"644\" height=\"346\" border=\"0\" \/><\/a><\/p>\n<p>Let\u2019s add a Java servlet to the project. Right click on the <em>src <\/em>folder in project explorer and select <em>New \u2013&gt; Servlet<\/em>, give the servlet a name and add it to the project.<\/p>\n<p><a href=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image1.png\"><img decoding=\"async\" loading=\"lazy\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" src=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image_thumb1.png\" alt=\"image\" width=\"644\" height=\"437\" border=\"0\" \/><\/a><\/p>\n<p>Open <em>MyServlet.java<\/em> and copy paste the below code in the <em>doGet()<\/em> function,<\/p>\n<pre class=\"lang:java decode:true \">protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {\r\n\r\n        response.setContentType(\"text\/html\");\r\n        response.setCharacterEncoding(\"UTF-8\");\r\n\r\n        try (PrintWriter writer = response.getWriter()) {\r\n\r\n            writer.println(\"&lt;!DOCTYPE html&gt;&lt;html&gt;\");\r\n            writer.println(\"&lt;head&gt;\");\r\n            writer.println(\"&lt;meta charset=\\\"UTF-8\\\" \/&gt;\");\r\n            writer.println(\"&lt;title&gt;MyServlet.java:doGet(): Servlet code!&lt;\/title&gt;\");\r\n            writer.println(\"&lt;\/head&gt;\");\r\n            writer.println(\"&lt;body&gt;\");\r\n\r\n            writer.println(\"&lt;h1&gt;This is a simple java servlet.&lt;\/h1&gt;\");\r\n\r\n            writer.println(\"&lt;\/body&gt;\");\r\n            writer.println(\"&lt;\/html&gt;\");\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>If you are seeing an error which says \u201c<em>java: try with resources is not supported in \u2013source 1.6<\/em>\u201d, go to project properties by right clicking on the project and selecting <em>Open Module Settings<\/em>, select <em>Project<\/em> on the left rail and change the Project Language Level to 8.<\/p>\n<p>Let\u2019s modify <em>index.jsp<\/em> to put an entry point to our servlet,<\/p>\n<pre class=\"lang:xhtml decode:true \">&lt;%@ page contentType=\"text\/html;charset=UTF-8\" language=\"java\" %&gt;\r\n&lt;html&gt;\r\n  &lt;head&gt;\r\n    &lt;title&gt;&lt;\/title&gt;\r\n  &lt;\/head&gt;\r\n  &lt;body&gt;\r\n    &lt;h1&gt;Simple Java Web App Demo&lt;\/h1&gt;\r\n    &lt;p&gt;To invoke the java servlet click &lt;a href=\"MyServlet\"&gt;here&lt;\/a&gt;&lt;\/p&gt;\r\n  &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Modify <em>web.xml<\/em> file and put the below servlet configuration in it, the url pattern is case sensitive so make sure it matches your servlet name exactly,<\/p>\n<pre class=\"wrap:true decode-attributes:false lang:xhtml decode:true \">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;web-app xmlns=\"http:\/\/xmlns.jcp.org\/xml\/ns\/javaee\"\r\n    xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n    xsi:schemaLocation=\"http:\/\/xmlns.jcp.org\/xml\/ns\/javaee http:\/\/xmlns.jcp.org\/xml\/ns\/javaee\/web-app_3_1.xsd\"\r\n    version=\"3.1\"&gt;\r\n\r\n  &lt;servlet&gt;\r\n    &lt;servlet-name&gt;MyServlet&lt;\/servlet-name&gt;\r\n    &lt;servlet-class&gt;MyServlet&lt;\/servlet-class&gt;\r\n  &lt;\/servlet&gt;\r\n\r\n  &lt;servlet-mapping&gt;\r\n    &lt;servlet-name&gt;MyServlet&lt;\/servlet-name&gt;\r\n    &lt;url-pattern&gt;\/MyServlet&lt;\/url-pattern&gt;\r\n  &lt;\/servlet-mapping&gt;\r\n\r\n&lt;\/web-app&gt;<\/pre>\n<p>Go to <em>Build \u2013&gt; Rebuild Project<\/em>, and make sure the project is building fine. Let\u2019s now package our application in WAR format (Web application ARchive) and deploy it on a machine running Tomcat.<\/p>\n<p>Right click on the project and select <em>Open Module Settings<\/em>, click on <em>Artifacts<\/em> on the left rail and select + to add a new artifact type. Click on <em>Web Application : Archive<\/em> and select the project name.<\/p>\n<p><a href=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image2.png\"><img decoding=\"async\" loading=\"lazy\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" src=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image_thumb2.png\" alt=\"image\" width=\"644\" height=\"348\" border=\"0\" \/><\/a><\/p>\n<p>Now when you build the project you will find file <em>SimpleJavaWebApp_war.war<\/em> generated under \\SimpleJavaWebApp\\out\\artifacts\\SimpleJavaWebApp_war folder.<\/p>\n<p>Let\u2019s deploy our app now, go to the machine where you installed Tomcat (it could be the same machine too), and under the Tomcat installation directory, copy the above WAR file under the <em>webapps<\/em> folder. For me the path is \u201c<em>C:\\Program Files (x86)\\Apache Software Foundation\\Tomcat 8.0\\webapps<\/em>\u201d. To make sure your app is working as expected, navigate to <a title=\"http:\/\/localhost:8080\/SimpleJavaWebApp_war\/\" href=\"http:\/\/localhost:8080\/SimpleJavaWebApp_war\/\">http:\/\/localhost:8080\/SimpleJavaWebApp_war\/<\/a> and check if the web page loads up correctly.<\/p>\n<p><a href=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image3.png\"><img decoding=\"async\" loading=\"lazy\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" src=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image_thumb3.png\" alt=\"image\" width=\"509\" height=\"257\" border=\"0\" \/><\/a><\/p>\n<p>So the bulk of the work is done. We\u2019ve created a simple Java web app, added a java servlet to it, deployed the application on Tomcat and made sure that the servlet code is invoked correctly. We\u2019ll now look at how to remotely debug this app. This is useful is cases where you have the application running on a server, and your code and source enlistment are on a different machine.<\/p>\n<p>To get remote debugging working, we need to instruct Tomcat to start the JVM in a \u201cdebug\u201d mode and then attach to the JVM from IDEA.<\/p>\n<p>Open Tomcat server properties, go to the Java tab and add the below entry under <em>Java Options<\/em> (make sure you add this in a new line),<\/p>\n<p><strong>-agentlib:jdwp=transport=dt_socket,address=1043,server=y,suspend=n<\/strong><\/p>\n<p><a href=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image4.png\"><img decoding=\"async\" loading=\"lazy\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" src=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image_thumb4.png\" alt=\"image\" width=\"500\" height=\"484\" border=\"0\" \/><\/a><\/p>\n<p>Restart the server and check if you can access <em>SimpleJavaWebApp<\/em> from a remote machine. I setup the server and deployed the war file on a different machine and navigated to below URL to check,<\/p>\n<p><a href=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image5.png\"><img decoding=\"async\" loading=\"lazy\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" src=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image_thumb5.png\" alt=\"image\" width=\"644\" height=\"284\" border=\"0\" \/><\/a><\/p>\n<p>We now need to create a debug configuration in IDEA to connect to this machine. Go to <em>Run \u2013&gt; Edit Configurations\u2026<\/em> Click on the + icon and add <em>Tomat Server \u2013&gt; Remote configuration<\/em>. Make sure you specify the host IP address correctly. You can also modify the \u2018Open Browser\u2019 option so that the java app launches when you start debugging.<\/p>\n<p><a href=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image6.png\"><img decoding=\"async\" loading=\"lazy\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" src=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image_thumb6.png\" alt=\"image\" width=\"644\" height=\"416\" border=\"0\" \/><\/a><\/p>\n<p>Switch to \u2018Startup\/Connection\u2019 tab and set the TCP port to the one you used while setting up Tomcat, 1043 in this case,<\/p>\n<p><a href=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image7.png\"><img decoding=\"async\" loading=\"lazy\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" src=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image_thumb7.png\" alt=\"image\" width=\"644\" height=\"416\" border=\"0\" \/><\/a><\/p>\n<p>Save the debug configuration and set a breakpoint in the <em>doGet()<\/em> function in <em>MyServlet.java<\/em> file. Now start debugging. You should see the web browser launch and when you click on the link to invoke the servlet, your breakpoint should be hit.<\/p>\n<p><a href=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image8.png\"><img decoding=\"async\" loading=\"lazy\" style=\"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;\" title=\"image\" src=\"https:\/\/www.techtwaddle.co.in\/blog\/wp-content\/uploads\/2015\/03\/image_thumb8.png\" alt=\"image\" width=\"644\" height=\"375\" border=\"0\" \/><\/a><\/p>\n<p>In case you see an error in IDEA which says \u2018unable to connect : connection refused\u2019, you might need a firewall exception for incoming connections on port 1043 (and 8080 too). So go to Windows Firewall settings and create an inbound rule on TCP port 1043 to allow incoming connections, and that should fix the problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had to get this setup up and running at work, thought it\u2019ll be a good idea to jot it down here. The first step is to install IntelliJ IDE from here. I installed the ultimate edition which has a free 30-day trial, but the steps below should work well with the free community edition &hellip; <a href=\"https:\/\/techtwaddle.co.in\/blog\/2015\/03\/29\/creating-a-simple-java-web-app-using-intellij-idea-and-setting-up-remote-debugging\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Creating a simple java web app using IntelliJ IDEA and setting up remote debugging<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[7,10,6],"tags":[18,17,19],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1ktFF-8d","_links":{"self":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/509"}],"collection":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/comments?post=509"}],"version-history":[{"count":20,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/509\/revisions"}],"predecessor-version":[{"id":733,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/posts\/509\/revisions\/733"}],"wp:attachment":[{"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/media?parent=509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/categories?post=509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techtwaddle.co.in\/blog\/wp-json\/wp\/v2\/tags?post=509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}