Wednesday, 11 June 2014

MCQ #15

Question :

Consider the following  code : 

class Super { static String ID = "QBANK"; }
class Sub extends Super{
static { System.out.print("In Sub"); }
}
public class Test{
public static void main(String[] args){
System.out.println(Sub.ID);
}
}

What  will  be the output  when  class Test  is run ?

A. I t  will  print  In Sub and QBANK.
B. I t  will  print  QBANK.
C. Depends on  the implementation  of  JVM.
D. It  will  not  even  compile.
E. None of  the above.

Answer

Answer is option B.

Explanation 

It would look like the program would print  "In sub" followed by QBANK and the option would be A but it is not. 

Since ID is inherited from the Super class it is really the Super class that is initialized and not the Sub class. So that static method in Sub class will not get printed. 

A class is initialized only when it's content (variables/methods) which are not inherited gets referenced. Not otherwise.

From JLS 12.4.1:
A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
  • T is a class and an instance of T is created.
  • T is a class and a static method declared by T is invoked.
  • A static field declared by T is assigned.
  • A static field declared by T is used and the field is not a constant variable (§4.12.4).
  • T is a top-level class, and an assert statement (§14.10) lexically nested within T is executed.

Tuesday, 10 June 2014

Change Tomcat Server's timeout in Eclipse

I got the following error on Eclipse while starting Apache Tomcat



So in this post I will show how can we increase the server timeout for Tomcat.

  • Go to the server view

  • Double click the Server. Configuration window would open. On the right hand side you could see Timeouts dropdown tab. You can go in that tab. It will have time limit for start and stop. Change it as per your need.

Default start time limit is 45 second where as stop limit is 15 secs.

Spring MVC Hello Wold Example

Goal

In this post I will demonstrate how to create a simple Spring MVC project. This will be an end to end demo. I will conclude the post by showing the working demo. I am going to use Apache Ivy for dependency management. For installing and using Apache Ivy you can go though my earlier post-
Installing and using Apache Ivy as dependency manager.

Getting Started

So lets get started.

  • Open up your eclipse. Note you need the EE version of eclipse as we will be needing server to run our project on. 
  • In Eclipse create a new project of type "Dynamic Web Project" (Screenshot below)
  • Next give your project a name. In my case I am naming my project GreeterProject.
  •  Ensure that you have selected Apache Tomcat 7.0 as your target runtime. If not select new runtime them select Tomcat 7. Next either provide the installation directory where you have installed the tomcat or if not eclipse will do that for you(Screnshots below)

  •  Once you have done this your project structure would look like -
  • Before we start coding lets add ivy dependency to the project. We will look into what dependency we need and what needs to go into ivy file but for now lets create one. Right click your project folder and select add -> Others -> Ivy file. It should ask you what container ivy file is for. Select out GreeterProject from the list. You can give organization and module name as per your wish.
  •  Check your WebContent/WEB-INF folder. You should have a web.xml file. This is the file where we provide all configurations needed for our project. If it is not present then create one. Its simple. Right click that folder-> new -> xml file -> name it web.xml.
  • Add the following content in the file

    <web-app id="WebApp_ID" version="2.4" 
        xmlns="http://java.sun.com/xml/ns/j2ee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
      <display-name>Spring Web MVC Demo Application</display-name>
      
      
      <servlet>
          <servlet-name>mvc-dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
      </servlet>
    
      <servlet-mapping>
         <servlet-name>mvc-dispatcher</servlet-name>
            <url-pattern>*.htm</url-pattern>
      </servlet-mapping>
      
    
    </web-app>
    
  • Lets understand what above configuration says. It defines a default servlet called mvc-dispatcher and it define a mapping. Tha mapping is also very simple to understand. It maps a servlet to a url-pattern. In above case it means on our server any url that comes if has the pattern *.htm then forward it to mvc-dispacher servlet to handle. If you see the class of the servlet we have mentioned it is org.springframework.web.servlet.DispatcherServlet. That is it is a dispacher servlet provided by Spring MVC framework. As the name suggests its work is to dispatch the requests to various modules called controllers. Don't worry we will come to it :). But for now get this that the mention servlet will simply handler the urls with pattern *.htm and will redirect it to different controllers for further processing. So where is this mapping of controller and url provided ?
  • It is provided in a spring configuration file. By default Spring MVC framework will search for a file name "dispacherServlet name" + "-servlet.xml" which in our case will turn out to be mvc-dispatcher-servlet.xml. So go ahead create this file under WEB-INF and put the following content in it.

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
        
    
        <bean id="viewResolver"
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix">
                <value>/pages/</value>
            </property>
            <property name="suffix">
                <value>.jsp</value>
            </property>
        </bean>
    
        <bean id="urlMapping"
            class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
            <property name="mappings">
                <props>
                    <prop key="/welcome.htm">testController</prop>
                </props>
            </property>
        </bean>
    
        <bean id="testController" class="TestController">
        </bean>
    </beans>
    

    This is just one way spring file are configured i.e if your servlet name is myservlet Spring framework will look for file named myservlet-servlet.xml. Other way is to manually provide the configuration files in the servlet tag itself in web.xml file

      <servlet>
          <servlet-name>mvc-dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
            <init-params>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/mservletconfig.xml</param-value>
            </init-params>
      </servlet>
    


    NOTE :  This may not be a good idea if you have multiple entries into you spring application i.e not just DispacherServlet , you may be supporting SOAP based web services. Providing spring initialization files in each servlet declaration as init-params is not necessary. In that case you can do

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mservletconfig.xml</param-value>
    </context-param>
    
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    <listener>
    


    This will essentially listen for all application contexts that are loaded and will load the common spring files for each entry point (multiple servlets for eg.).
  • Lets try to understand the content of this file. Lets start from the bottom. We have a controller name testController and it's class is TestController. But wait .... where is this TestController class? That's right you need to create one. But just in a while. Lets understand all the content in the spring config file. Next what you see is a bean with mapping. Class corresponds to functionality that is provided by Spring MVC itself. So nothing to worry about. In mapping you can see the key and the value between the tag. What does it say ?

    It simply says if the incoming URL has /welcome.htm at the end forward its request to testController. So to sum the this up first the request comes to the server(actually tomcats container name Catalina). As mentioned in web.xml if the url is of patter *.htm request will be forwarded to mvc-dispatcher servlet. This servlet will forward the request to testController if it has /welcome.htm in the url end.

    Lastly what you see is a bean called resolver. Class is again of that provided by Spring. You see something called prefix and suffix tags. What a controller returns is a ModelAndView object. It has a name. What view resolver does is that it bundles this name in between prefix and suffix and creates a path. This path points to a JSP file which is then sent to the client(browser).

     
  • Enough of the theory lets get started. First create a class called TestController in the src folder and add the following content in the file.

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.AbstractController;
    
    
    public class TestController extends AbstractController {
        
        @Override
        protected ModelAndView handleRequestInternal(HttpServletRequest request,
                HttpServletResponse response) throws Exception {
            System.out.println("Entered TestController");
            ModelAndView model = new ModelAndView("greetings");
            return model;
        }
    
    }
    
     
    
  •  So our class extends a class AbstractController. And in this we override a method called handleRequestInternal() which handles the request. Here we are creating an ModelViewObject with name "greeting" and returning it. Now read the previous to previous point again. Our View resolver will now be looking for a file called /pages/greetings.jsp to send to the client/browser.
  • I wrote the class but I am seeing a lot of red lines. None of the classes are getting resolved. Why would it be? We are talking about Spring MVC for some time now but where is the framework/jar? Thats right its time to use Ivy. Open the ivy file and add following content -

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!--
       Licensed to the Apache Software Foundation (ASF) under one
       or more contributor license agreements.  See the NOTICE file
       distributed with this work for additional information
       regarding copyright ownership.  The ASF licenses this file
       to you under the Apache License, Version 2.0 (the
       "License"); you may not use this file except in compliance
       with the License.  You may obtain a copy of the License at
    
         http://www.apache.org/licenses/LICENSE-2.0
    
       Unless required by applicable law or agreed to in writing,
       software distributed under the License is distributed on an
       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
       KIND, either express or implied.  See the License for the
       specific language governing permissions and limitations
       under the License.    
    -->
    <ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
        <info
            organisation="OpenSourceForGeeks"
            module="Greeter"
            status="integration">
        </info>
        <dependencies>
        
            <dependency org="org.springframework" name="spring-webmvc"
                rev="4.0.4.RELEASE">
            </dependency>
            
        </dependencies>
    </ivy-module>
    
     
    
  • After this right click Ivy file and select add ivy library. This will automatically download Spring MVC and add it to your classpath. After ivy resolution is complete you will see all errors gone.
  • Next lets complete our directory structure. Create a folder called pages (remember the prefix ? )under WebContent folder and add greetings.jsp file in it with following content.

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Spring Demo Example</title>
    </head>
    <body>
    <h1><b><i>Hello World!! Welcome to Spring MVC Demo!!</i></b></h1>
    </body>
    </html>
    
  •  Just to make sure that we are in sync till now lets check that our directory structures map.



  • Looks like we completed all requirement for running our demo. Go ahead fire up the server. Right click the project-> Run as - > Run on server -> Select our Apache tomcat 7 that we configured earlier.

     
  • Opss!!!! What on earth is this exception

    java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet I remember I added the Spring in ivy and it did not complain on compilation. So why sudden exception on running. Well that actually should give you the hint. Our library is available at compile time but not at runtime. It's really very simple to resolve this. You need to add ivy to deployment assembly for runtime, I have documented steps to do this with detailed screenshots in s separate post. You can go through it(Click this link). That should resolve that error.
  • Lets run it again. Works fine?? You should not see any errors and see something like server startup

  •   We are all set now lets test our project. Open the URL "http://localhost:8080/GreeterProject/welcome.htm" in your web browser. You should see
  • If you are getting
    java.lang.LinkageError: javax.servlet.jsp.JspApplicationContext.getExpressionFactory
    error do the following changes in the ivy -

            <dependency org="org.springframework" name="spring-webmvc"
                rev="4.0.4.RELEASE">
                <exclude org="javax.servlet" name="javax.servlet-api" />
                <exclude org="javax.servlet.jsp" name="jsp-api" />
                <exclude org="javax.el" name="javax.el-api" />
            </dependency>
    


    and it should work. You can also see my answer for this error on Stack Overflow for more information.

Related Links

Saturday, 7 June 2014

Open a new web page to show content using javascript.

Goal

In my last post I showed how can we get started with Web development by making a simple "Hello World" HTML web page. In  this post we will see how can we open a new window to show some content.

Getting Started

 Lets first create a web page that meets our requirement and then we can look at the javascript part.
Again create a file called index.html and add following content in it.

<html>
<header><title>Open Source For Geeks Tutorial</title></header>
<body>
<center>
<h1>Open Source For Geeks Demo</h1>
<table>
<caption>Demo Table</caption>
<tr>
<td>
Enter Some Text : 
</td>
<td>
<input type="text" name="textContent">
</td>
</tr>
<tr>
<td>
Click to Submit : 
</td>
<td>
<input type="button" value="Submit">
</td>
</tr>
</table>
</center>
</body>
</html>



Now view the page in the browser. You should see something like below


What we want to achieve is to enter some text in the text field then click the Submit button and we should be able to see the text content entered on a new page.

Following code is fully functional with javascript function.

<html>
<header>
<title>Open Source For Geeks Tutorial</title>
<script>
function openNewPage() {
    top.consoleRef=window.open('','myconsole',
        ',menubar=0'
       +',toolbar=1'
       +',status=0'
       +',scrollbars=1'
       +',resizable=1');
    top.consoleRef.document.writeln(
      '<html><head><title>Open Source For Geeks Tutorial</title></head>'
       +'<body bgcolor=white onLoad="self.focus()">'
       + '<h1><b><i>This is a new Window!</i></b></h1>'
       + '<textarea rows="40" cols="100">'
       + document.getElementById("SubmitTextID").value
       + '</textarea>'
       +'</body></html>'
     );
    top.consoleRef.document.close();
}
</script>
</header>
<body>
<center>
<h1>Open Source For Geeks Demo</h1>
<table>
<caption>Demo Table</caption>
<tr>
<td>
Enter Some Text : 
</td>
<td>
<input type="text"  id="SubmitTextID" name="textContent">
</td>
</tr>
<tr>
<td>
Click to Submit : 
</td>
<td>
<input type="button" value="Submit" onclick="openNewPage()">
</td>
</tr>
</table>
</center>
</body>
</html>


and lets try it out

Enter some text in the text field and click Submit button. The entered text should now come in the new window.



Now click the submit button. New Window will open as shown below


That was it! It's that simple.Just a java script function. There is a reason I have put the content inside <textarea> tag. This is because you can even have tags inside text area and they will be displayed as tag. That means if your text has <b> in it same will be displayed and will not be interpreted as bold tag. This was just a simple example but you can show complex data that is fetched from the server. You can also show some xml content in it.

Carefull!!!!

Be careful though. document.writeln() method takes a html input. So all your escaped characters will be "un-escaped". Meaning if your display text contains something like &amp; it will be replaced by  & and shown. If you really want to show the escaped characters you need to do something like below - 

function writeConsole(content,idx) {

     top.consoleRef=window.open('','myconsole' + idx,

        ',menubar=0'

       +',toolbar=1'

       +',status=0'

       +',scrollbars=1'

       +',resizable=1');

     top.consoleRef.document.writeln(

      '<html><head><title> Generated Content</title></head>'

       +'<body bgcolor=white onLoad="self.focus()">'

       + '<textarea rows="500" cols="500">'

       + '</textarea>'

       +'</body></html>'

     );

     top.consoleRef.document.close();

    

     var doc = top.consoleRef.document;

     var textarea = doc.getElementsByTagName('textarea')[0];

     textarea.value = content;

    }


I encountered this scenario and corresponding stack overflow question asked is

HTML Hello World Tutorial

Introduction

Our live as we are living now would not be the same without the internet. What we see while browsing internet are called web pages. What we use to browse these pages is called a browser (client). These pages are written using various technologies like HTML, CSS, JavaScript, JQuery etc. which are client side technologies. There are various backed (server side) technologies like JSP, Servlets that are used to build application that are deployed on the servers eg. Tomcat which help server users/clients request to browse a particular page. What user types in an address bar to fetch a web page is called URL(Universal resource locator). For example you may type http://google.com/. What you see before colon ':' (i.e http or Hypertext Transfer Protocol) is the protocol used to communicate with the server. It can also be https which is secure version of http protocol.You may see the use of this protocol when you browse a page to do some secure action like payments or transactions. Both HTTP and HTTPS fall under application layer in OSI model. Both of them have TCP as their underlying protocol. So basically when browser it to request a web page it first creates a TCP connection with the server and then uses HTTP or HTTPS to get the required pages to display.

Goal

Most of the basic tutorials can be found on W3Schools. Prior to moving to some advance tutorials on web development let me provide this basic Hello World HTML tutorial. Here we will simply crate a web page and open it in our browser that shows "Hello World!" text.

Getting Started

  • Create a file name index.html . Notice the extension html. It is required so that the browser and OS understands it is a web page and will be opened in a web browser.
  • Next add the following text in your file

    <html>
    <header><title>Open Source For Geeks Tutorial</title></header>
    <body>
    <center>
    <h1><b><i>Hello World!!</i></b></h1>
    </center>
    </body>
    </html>
    
  • Simply double click the file. It should open up in your default browser and should look like below

  • See how easy that was? Web pages that you visit in day to day life are much more complex. They have animations, dynamic contexts, logins etc but it all starts from Hello world :)

Understanding the language

  1. HTML is stands for Hyper Text Markup Language. You have bunch of tags that the browser understands and renders.
  2. Notice the <html></html> tag. This is the root element of your web page. Or kind of was. Modern browsers detect the html page even if this tag is not present.
  3. Next comes your tag <head></head> and <body></body>. As the name suggests head tag has your meta info like page titile. you can put it under <title></title> tag in your head. Notice the text on the tab in above image. Your java scripts also go in this head section.
  4. Body as the name suggests has your actual page content to be shown. Here we are simply displaying "Hello World !!" text. But notice the font ? Looks different ? That is because I used some tags.
  5. <center><center/> tag tell the browser that the element/tag that come in between must be rendered in the center of the browser. <b> is for bold and <i > is for italics. <h1> is the size tag that shows text with different size. You can go ahead and try <h2>... etc.
As I told earlier you will find much help on W3Schhools.  So do go through it if you are beginning with Web development. Let me know if you still have any doubts.
t> UA-39527780-1 back to top