Showing posts sorted by relevance for query jsp. Sort by date Show all posts
Showing posts sorted by relevance for query jsp. Sort by date Show all posts

Monday, June 24, 2019

JSP Scripting elements



JSP Scripting elements

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<h1>Java Server Pages</h1>

<h2><a href="scriptlets.jsp">JSP Scripting Elements </a> </h2>

<h2><a href="implicit.jsp">Implicit Objects </a></h2>

<form action="implicit.jsp"> 
 Name: <input type="text" name="name"> 
<input type="submit" value="Submit"><br/> 
</form>

</body>

</html>



scriptlets.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>


<h2> JSP Scripting Elements </h2>

<h2>JSP scriptlet tag </h2>
<% out.print("Welcome to JP"); %> 
<br/>
<% out.print("Welcome to JSP "+ request.getParameter("name")); %> 
<br/>



<h1> JSP expression tag </h1>
<!-- need not write out.print() -->
 <%= "Todays Date " %>
 <br/>
 <%=java.util.Calendar.getInstance().getTime() %>

<h1>JSP Declaration Tag</h1>
<%! int data=10;%> 
<%= data %>

</body>
</html>


How it interprets the data, check below:

scriptlets.jsp

JSP Scripting Elements

JSP scriptlet tag

<% out.print("Welcome to JP"); %>
<% out.print("Welcome to JSP "+ request.getParameter("name")); %>

JSP expression tag

<%= "Todays Date " %>
<%=java.util.Calendar.getInstance().getTime() %>

JSP Declaration Tag

<%! int data=10;%> <%= data %>

Tuesday, June 25, 2019

JSP Implicit Objects Presentation

JSP Implicit Objects Presentation

https://github.com/studyskymate/J2EEDemos/tree/master/WebContent/jspDemos



Question: Explain include Directive and include Action of JSP. Answer:   This is a very popular interview question on JSP, which has been asked from a long time and still asked in the various interview. This question is good to test some fundamental concept like translation of JSP and difference between translation time and run time kind of concept. Syntax for include Directive is : <%@ include file="fileName" %> which means we are including some file to our JSP Page when we use include directive contents of included file will be added to calling JSP page at translation time means when the calling JSP is converted to servlet ,all the contents are added to that page one important thing is that any JSP page is compiled if we make any changes to that particular page but if we have changed the included file or JSP page the main calling JSP page will not execute again so the output will not be according to our expectation, this one is the main disadvantage of using the include directive that why it is mostly use to add static  resources, like Header and footer .  Syntax for include action is  <jsp:include page=”relativeURL” />  It is a runtime procedure means the result of the JSP page which is mentioned in relative URL is appended to calling JSP at runtime on their response object at the location where we have used this tag. So any changes made to included page is being effected every time, this is the main advantage of this action but only relative URL we can use here, because request and response object is passed between calling JSP and included JSP.

JSP Implicit Objects Presentation



JSP Introduction presentation

JSP Introduction presentation


JSP Introduction


A JSP page is a text document that contains two types of text: static data, which can be expressed in any text-based format (such as HTMLSVGWML, and XML), and JSP elements, which construct dynamic content.

The recommended file extension for the source file of a JSP page is .jsp. The page can be composed of a top file that includes other files that contain either a complete JSP page or a fragment of a JSP page. The recommended extension for the source file of a fragment of a JSP page is . jspf.


A JavaServer Pages component is a type of Java servlet that is designed to fulfill the role of a user interface for a Java web application. Web developers write JSPs as text files that combine HTML or XHTML code, XML elements, and embedded JSP actions and commands.

JSP Introduction presentation





Friday, June 21, 2019

Request Dispatcher Servlet example in creating simple Login Form


Use Request Dispatcher Servlet in creating simple Login Form


We are going to create a Login Form Here. At the end of this section, you will be able to use Request Dispatcher Servlet to forward the request to another webpage based on your inputs.


Let us Start Step by Step:

Step 1: 


Create a Dynamic Web Project in IDE. For Example, here I am using eclipse to create a Dynamic Web Project.

If you do not have eclipse use below link to download the eclipse for Enterprise Edition.:
https://www.eclipse.org/downloads/packages/release/kepler/sr2/eclipse-ide-java-ee-developers






Give Proper Name to your project according to you.

Here in the Target runtime (Picture below), you can see Apache Tomcat v8.5. It is Server which is required to run Dynamic Web Applications or Enterprise Applications.


If it is not present or shown Empty then you have to Download the Server
i.e Apache Tomcat or Weblogic or any other server and have to link in the New RunTime Button Link.





Next Click on Next and Finish.


Below is the Final Structure of the project.





You need to create below files:


1. Login and Success Servlet Class.


2. home and success.jsp for the login page and if credentials are correct then forward it to the success page.



Step 2:


Create home.jsp file under webContent.








Content of home.jsp file.

It contains the code for Simple Login Form.

<form action="Login" method="post">

Here action means when user submit this form then Login Servlet will be invoked and it find the post method in the Login Servlet


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

      <h1>Login form</h1>
      <div>
            <form action="Login" method="post">
                  <table>
                        <tr>
                              <td>Name :</td>
                 <td><input type="text" name="userName" />                                  </td>
                        </tr>
                        <tr>
                              <td>Password :</td>
                <td><input type="password" name="userPass"/></td>
                        </tr>
                        <tr>
                 <td><input type="submit" value="login" /></td>
                        </tr>
                  </table>
            </form>
      </div>

</body>

</html>

home.jsp file snapshot





Step 3:


Create Login.java Servlet











Login Servlet:


package com.studyskymate.dinesh.servlet.login;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Login")
public class Login extends HttpServlet {
      private static final long serialVersionUID = 1L;
      
    public Login() {
        super();
    }

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           
             response.setContentType("text/html"); 
             PrintWriter out = response.getWriter(); 
             String name=request.getParameter("userName"); 
             String password=request.getParameter("userPass"); 
             
       if(name.equals("dinesh") && password.contentEquals("1234") )               {
        RequestDispatcher rd  =request.getRequestDispatcher("success");

    //To handle it you need to create success servlet which we will create in next step
         rd.forward(request, response);
        }else {
       out.print("Sorry Invalid UserName or Password !");
       RequestDispatcher rd=request.getRequestDispatcher("/home.jsp");
           rd.include(request, response);
             }
             
      }


}


----------------------------------------------------------------------------------------------------------

Step 4
Create Success.java Servlet Similarly:

It will Invoke Login Servlet and if username and password are correct then it will forward the request to Success.jsp

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/success")
public class Success extends HttpServlet {
      private static final long serialVersionUID = 1L;

    public Success() {
        super();
    }


      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

          response.setContentType("text/html");
          PrintWriter out= response.getWriter();
      RequestDispatcher rd=request.getRequestDispatcher("/success.jsp");
          rd.forward(request, response);
      }


}

-----------------------------------------------------------------------------------------------------------------


Step 5.

Create success.jsp to capture the response from Success Servlet.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<div>
<h1>Welcome   <font color="red"><%=request.getParameter("userName") %> </font> </h1>
<h2>You are Valid User</h2>
<a href="/WebProject/home.jsp"><button type="button">Home Page</button></a>
</div>

</body>
</html>


----------------------------------------------------------------------


Step 6

web.xml file.

6.1 create web.xml file under WEB-INF folder if not present.
6.2 Add welcome file home.jsp or your index file statement in the web.xml
as below:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>WebProject</display-name>
  <welcome-file-list>
    <welcome-file>home.jsp</welcome-file>
  </welcome-file-list>
</web-app>


--------------------------------------------------------------------


Step 7: 
Run your project on Server.










Below Login Form will open after running the server at your browser.






Enter the correct Username and password.
In my case, it is dinesh and 1234 because I have provided these validations in Login Servet.
Now It will Invoke Login Servlet and if username and password is correct then it will forward the request to Successjsp






In the case of Wrong credentials, it will include the output of login servlet and forward the request to home.jsp




Thanks



Create a Digital Clock using HTML and JavaScript

Create a Digital Clock using HTML and JavaScript  <! DOCTYPE html> < html > < head > <...

Followers

Search This Blog

Popular Posts