Tuesday, July 27, 2021

What is JSESSIONID in Java Web application - JSP Servlet? Example

What is JSESSIONID in JSP-Servlet
JSESSIONID is a cookie generated by Servlet containers like Tomcat or Jetty and used for session management in the J2EE web application for HTTP protocol. Since HTTP is a stateless protocol there is no way for Web Server to relate two separate requests coming from the same client and Session management is the process to track user sessions using different session management techniques like Cookies and URL Rewriting. If a Web server is using a cookie for session management it creates and sends JSESSIONID cookie to the client and then the client sends it back to the server in subsequent HTTP requests.

How to Export HTML to PDF, CSV, and Excel using Display Tag in JSP? Example

Display tag provides export options to export page into PDF, CSV, Excel, and XML in Java web application written using JSP, Servlet, Struts, or Spring MVC framework. Display tag is a tag library and you just need to use it in your JSP page to display tabular data or generate HTML tables dynamically. Earlier we have seen 10 display tag tips for getting most out of display tag and in this JSP Servlet tutorial we will see one display tag export issue, which prevents display tag export functionality to work properly.

How to Configure HTTPS (SSL) in Tomcat 6 and 7 Java Web Server? Example

Setting SSL (Secure Socket Layer) in Tomcat is often a requirement, especially while developing secure web applications, which requires access over HTTPS protocol. Since the Tomcat web server doesn't provide SSL settings by default, you need to know how to configure SSL in Tomcat, and even worse it varies between different tomcat versions. for Example SSL setup which works on tomcat 6, doesn't work as it is in tomcat 7. In this article, we will see, how to configure tomcat for HTTPS in both tomcat 6 and 7. For those programmers who are not very familiar with SSL and HTTPS here is a quick overview of SSL, certificates and HTTPS, and I suggest reading that article to get a better understanding of How SSL works and How websites are accessed securely over the internet.

JDBC - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver Solution

"java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"  is a frequent Exception Java programmer face while writing JDBC connectivity code for MySQL, especially if you are trying to establish a JDBC connection to MySQL database first time. we have touched base on ClassNotFoundException in MySQL on our earlier articles on how to resolve ClassNotFoundException in Java and here we will see in detail what causes "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver" and how to fix com.mysql.jdbc.Driver error in Java.

File Upload Example in Java using Servlet, JSP and Apache Commons FileUpload - Tutorial

Uploading Files to the server using Servlet and JSP is a common task in Java web applications. Before coding your Servlet or JSP to handle file upload requests, you need to know a little bit about File upload support in HTML and HTTP protocol. If you want your user to choose files from the file system and upload them to the server then you need to use <input type="file"/>. This will enable us to choose any file from the file system and upload to a server. 

How to Split String in JSP? JSTL forTokens Tag Example

JSTL forTokens tag is another tag in core JSTL library to support Iteration or looping. It effectively compliments, more useful <c:forEach> tag, by allowing you to iterate over comma-separated or any delimited String. You can use this tag to split string in JSP and can operate on them individually. forTokens tag has similar attributes like forEach JSTL tag except one more attribute called delims, which specifies delimiter. For example to iterate over colon-separated String "abc:cde:fgh:ijk", delims=":". 

How to test Exception thrown by a Java Method in JUnit? Example

One part of unit testing a Java method is checking exceptions thrown by that method. A Java unit test should verify the correct exception thrown in the exceptional case and no exception should be thrown in a normal case. In JUnit 3.XX, there was no direct support to pass or fail a test based upon the exception thrown by a Java method. JUnit4 address this issue and provides an easy, and readable way to test exceptions thrown by a Java method. There are many situations when you need to test the exception thrown by any method in Java. A classical example of this is testing API methods which should throw IllegalArgumentException if arguments passed to the method are not matching to pre-conditions.

10 examples of displaytag in JSP, Struts and Spring

Display tag is one of the best free open source libraries for showing data in tabular format in a J2EE application using jsp, struts, or spring. it is shipped as a tag library so you can just include this in your jsp and add the corresponding jar and their dependency in class-path and you are ready to use it. The display tag is my favorite solution for displaying any tabular data because of its inherent capability in paging and sorting. It provides great support for pagination on its own but it also allows you to implement your own pagination solution. On the Sorting front, you can sort your data based on any column, implement default sorting, etc.

How to change Tomcat default port 8080? Example Steps

Tomcat default port is 8080 but many times other Java application also uses 8080 like any other web-server Resin or Jetty and starting tomcat may result in java.net.BindException:Address already in use: JVM_Bind 8080. In order to avoid this exception you can change default port of tomcat from 8080 to some other port e.g. 8081 or 8082. Though don't change to tomcat port which is likely to be used by tomcat itself e.g. 8443 is used by tomcat https port. Use port which is most likely to be free. In this tomcat tutorial we will see how to change default port 8080 for http protocol in tomcat and port 8443 port for https protocol in tomcat.

Monday, July 26, 2021

How to define Error page in Java Web Application - Servlet JSP Example

There are two ways to define the Error page in Java web application written using Servlet and JSP. The first way is page wise error page which is defined on each jsp page and if there is any unhandled exception thrown from that page, the corresponding error page will be displayed. The second approach is an application-wide general or default error page which is shown if any Exception is thrown from any Servlet or JSP and there is no page-specific error page defined.

How to Disable submit button in HTML JavaScript? Multiple Form Submission Solution Example

Avoiding multiple submissions of HTML form or POST data is a common requirement in Java web applications. Thankfully, You can prevent multiple submissions by disabling the submit button in HTML and JavaScript itself, rather than handling it on the server-side. This is a very common requirement while developing a web application using Servlet and JSP or any other web technology. At the same time, there is a lot of information and noise available in web, and its very hard to find a simple approach to the disable submit button. When I search for a simple way to disable the submit button in HTML and JavaScript, I was overwhelmed by responses on forums and various online communities.

JSTL foreach tag example in JSP - looping ArrayList

JSTL  foreach loop in JSP
JSTL  foreach tag is pretty useful while writing Java free JSP code.  JSTL foreach tag allows you to iterate or loop Array List, HashSet or any other collection without using Java code. After the introduction of JSTL and expression language(EL) it is possible to write dynamic JSP code without using scriptlet which clutters jsp pages. JSTL foreach tag is a replacement of for loop and behaves similarly like foreach loop of Java 5 but still has some elements and attribute which makes it hard for first-timers to grasp it. JSTL foreach loop can iterate over arrays, collections like List, Set and print values just like for loop.

JSP - How to check if ArrayList is Empty using JSTL Example

There are multiple ways to check if an ArrayList is empty in JSP or not. For example, you can use the empty operator and the length function of JSTL to check if a list is empty or not. You can also use scriptlet, which allows you to insert Java code into JSP but that is not advisable because it makes it harder to maintain a JSP page. You can also write a custom tag but that is not required because JSTL provides you the necessary support. In this article, I'll show you two ways to check if a given list or ArrayList is empty or not on the JSP page without using the scriptlet. You guessed it right, we will use JSTL, the Java standard tag library to solve this problem.

Difference between Servlet and JSP? Example

Servlet and JSP are two of the most popular Java web technologies to generate dynamic content in Java web applications there is some key difference between them. For example, Servlet is designed for Java developers as most of the coding is done in Java, while JSP was designed and developed for web GUI developers e.g. guys who write HTML and JavaScript and that's why you see the coding in JSP is tag-based. Even though you can use HTML tags as String inside Servlet and Java code as Scriptlet inside JSP, both are considered as bad practices and should be avoided at all costs because they are very hard to maintain.

How to get ServletContext in Servlet, JSP, Action class and Controller? Example

How to get ServletContext in Servlet, jsp, spring controller or struts action class is common to need of any Java web developer. As ServletContext is an application-wide object and used to store variables in the global scope, getting a reference of ServletContext is pretty important. Every web application can have only one ServletContext though they can have multiple ServletConfig objects. In this article we will see :

How to get Servlet Context inside Spring MVC Controller?
How to find  Servlet Context inside Struts Action class?
How to get Servlet Context inside JSP File?
How to find Servlet Context inside HttpServlet Class?