All about Tomcat

If you are seeing a 404 response on your HTTP request for an application running on a tomcat server, it may be because we need to have at least one ROOT.war in your /webapps folder.

If you want to change the port number on which tomcat is running, you need to edit properties in server.xml file in /conf folder : Update the connector port = “8080” to whatever port you want the tomcat to run on.

<Connector port="8080" protocol="HTTP/1.1" 
           connectionTimeout="20000" 
           redirectPort="8443" />

Some of the configuration for local tomcat installation are also present in catalina.sh file in  /apache-tomcat-8.0.32/bin directory.

If you are seeing any weird errors in your tomcat logs upon starting tomcat server, make sure you did not update any tomcat config files(Try to get that standard property file content from apache website).

Debugging java application in tomcat using Eclipse and Intellij :

Add this to your Tomcat catalina.sh file:

 JAVA_OPTS=”$JAVA_OPTS -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y”

Run Tomcat from command line in tomcat bin directory, run:

./catalina.sh start ; tail -f ../logs/catalina.out

If using Eclipse, create a Debug configuration:

  1. Under the “Run” menu, select “Debug”
  2. Single-click the heading “Remote Java Application”, then press the new button (looks like a page with a plus on it).
  3. On the dialog that appears, enter a meaningful name (like “Debug Tomcat” for this configuration).
  4. From the same dialog, change to the “Source” tab, then click “Add”
  5. On the dialog that appears, single-click “Java Project”, then click “OK”.
  6. On the dialog that appears, click “Select All” (or check the projects you wish to import), then click “OK”.
  7. Now click the “Connect” tab and check the box marked “Allow termination of remote JVM”.
  8. Set the Connection Properties to “localhost” and port 8787.  Note this is the same port as in the line you added to the catalina.sh file above.
  9. Click “Apply” to save your changes.
  10. Click “Debug” to start testing your configuration.

To deploy new changes in your webapp code to Tomcat using a Gradle task:

Eclipse:

  1. Under the “Run” menu, choose “External Tools”> “External Tools Configurations”
  2. Click the + add icon in the upper left corner
  3. Add the following Gradle tasks:

clean compileJava compileTestJava assemble  deployToTomcat

Make sure those tasks are defined in the build.gradle file for your project.

4.Click “Run”

in IntelliJ, create aDebug configuration:

  1. Under the “Run” menu, select “Edit Configurations”
  2. Click the + icon in the upper left corner
  3. Choose “Remote”
  4. Give the Configuration a name, such as “Debug Tomcat”
  5. Change the Port # to whatever your Tomcat service is listening on which would be 8787 in example above (dt_socket,address=8787 in the JAVA_OPTS )
  6. Click “OK”
  7. Click Run > Debug and choose this configuration.
  8. Add breakpoints to the code you wish to invoke/debug.

To deploy new changes in your webapp code to Tomcat using a Gradle task:

IntelliJ:

  1. Install the Gradle plugin for IntelliJ if you don’t already have it.
  2. Open the Gradle Projects window on the right side of the window
  3. Click the + icon at the top right and navigate to your project’s build.gradle file and select it.
  4. Expand the “Tasks” > “build” and “other” items in the tree, and run the tasks you wish to run and deploy the code to tomcat. (You can also run gradle tasks from terminal)

 

REST Web Services

Best Practices to follow while writing REST WS :

  1. http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api
  2. http://blog.mwaysolutions.com/2014/06/05/10-best-practices-for-better-restful-api/

Form encoding while making POST requests : When you make a POST request, you have to encode the data that forms the body of the request in some way.

HTML forms provide three methods of encoding.

  • application/x-www-form-urlencoded (the default)
  • multipart/form-data
  • text/plain

Use multipart/form-data if the request contains a File upload and it should be used in combination with the INPUT element, type=”file”.
Use application/x-www-form-urlencoded otherwise, which is the default if you omit enctype header in the request.
NEVER use text/plain as it only used if you want to debug the payload you are sending as part of HTTP request.

URL Encode Query params : When sending requests to REST API from POSTMAN, make sure you URL-Encode the query parameters part of the request.

CORS : Cross origin Resource Sharing : (https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)
When a request is made from browser to fetch data from an API, Origin header is always sent along with the request. Origin denotes the source of the request. This origin is parsed and verified at API to determine if it can respond with data.

For Http requests that can cause side effects on server data like PUT, DELETE, browser sends an OPTIONS request(Preflight request) before sending the actual request. When API gets the OPTIONS request, it then responds with details like allowed methods, allowed headers, etc – only then browser makes the actual PUT/DELETE requests to API.

Support infinite scrolling via REST API : https://developer.twitter.com/en/docs/tweets/timelines/guides/working-with-timelines

 

 

Mac Most Used Config files

These are the files that store most of the configuration information needed by the applications running on a Mac :

  1. ~/.ssh/config : File stores all SSH replacements

  2. ~/.bash_profile : All Alias are managed in the file.
    If you were to make any changes to this file and you want to see the changes take into effect immediately : source ~/.bash_profile

  3. /etc/hosts : Contains all the hosts information.

Hello world!

This is my very first blog post. I plan to share information here that can make things easier for people either related to technology.

I said I will write about technology : Why? because technology is fun.. be it Object oriented programming, java, or may be any other language : setting up environments/IDE’s for writing code is sometimes(if not always) a pain/Not fun. Hopefully information I post here will simplify things for you.