Git Product home page Git Product logo

basic-jsp-example's Introduction

#This project contains very basic JSP example without complicated code.

This projects helps one understand the basic working of JSP. We don't write any servlets in this project.

Operations

1)Takes username similar to basic-servlet-example and takes a number.
2)Outputs welcome {username} and multiplication tables

Note: In basic-servlet-example project, inside servlet class(TestSessionServlet) we had to write html string(code) inside out.write(). We had to get the username and append to the html string which is inside out.write(). In JSP we write java code inside html code, we get the username which is in request inside html code by writing java code.

Points to be noted

JSP lifecycle
  1. Translation of JSP to Java (servlet).
  2. Compilation of java to .class.
  3. Loading of servlet class.
  4. Instantiation(no-arg constructor).
  5. Initialization(init method).
  6. Invoke service() method in a new thread of execution(which in turn invoke doXXX methods).
  7. Destroy(destroy method)

Note: Since servlet is a singleton, from 2nd request first 5 steps will not be performed. From 2nd request service() method is invoked in a new thread of execution.

<% %> -> Scriplet tag(multiline java code) is copy pasted as is in service method.
<%= %> -> Expression tag goes inside out.print(executable java code). Executable java code is executed and returned to out.print()
<%! %> -> Declartion tag is copy pasted as is inside converted java servlet class but outside service() method.

Note: This project needs to be refactored to use best practices which will be done later. As of now this code should be used only for understanding purposes.

Converted JSP to JAVA looks like below(refer welcome.jsp)

...
...
out.write("<h3>Welcome ");  
out.print(request.getParameter("uname") );  
out.write("</h3>\r\n");  
out.write("<pre>\r\n");
String value = request.getParameter("number");
try
{
	Integer number = Integer.parseInt(value);
	for(int i=1;i<=10;i++)
	{
		out.println(number + " * " + i +" = " + (number*i)  );
	}
}
catch(NumberFormatException e)
{
	out.println("Invalid number");
}

     out.write("\r\n");
     out.write("<pre>\r\n");
...
...

basic-jsp-example's People

Contributors

jgpreetham avatar dmanjunath03333 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.