Java & JEE Training
Session 28 – Servlets - Part 4
Page 1Classification: Restricted
Agenda…
• SQL Injection continued… + Exercises
• Validating forms
• Redirecting requests to other sources
Page 2Classification: Restricted
Review of previous session…
….
Page 3Classification: Restricted
Web Server and Application Server
Presentation Tier:
JSP, Servlets
Run on Web Server
or container
JDBC on web server
or app server
Database
Client:
HTML JS CSS
(Run on client
machine on the
browser)
Page 4Classification: Restricted
Demo of complete Web app flow…
• Form which takes two fields… Student ID and Student Name…
• Output should show whether that row exists or not, and if it exists, how
many records in the table match the criteria (id, name)
• If match found, then print login successful, else login failed.
Validation in web apps
Page 6Classification: Restricted
Validating forms using JavaScript..
Best practice
• MUST always validate both on client side as well as server side.
• Validation in the client side is done using JavaScript.
• Validation in the server side is done using Java.
• Demo of frontend and backend validation.
• Example of JS validation.
• Check if value entered is a number or not:
if (data === parseInt(data, 10))
alert("data is integer")
else
alert("data is not an integer");
Page 7Classification: Restricted
More JS Validation Examples….
• Let us look at some validation examples…
Page 8Classification: Restricted
Check for non-empty field…
// If the length of the element's string is 0 then display helper message
function required(inputtx) {
if (inputtx.value.length == 0) {
alert("message");
return false;
}
return true;
}
Page 9Classification: Restricted
Check for restricting length of input field…
function lengthRange(inputtxt, minlength, maxlength)
{
var userInput = inputtxt.value;
if(userInput.length >= minlength && userInput.length <= maxlength){
return true;
}
else{
alert("Please input between " +minlength+ " and " +maxlength+ "
characters");
return false;
}
}
Page 10Classification: Restricted
Reference for more validation examples…
• A good reference for form validation..
http://www.w3resource.com/javascript/form/javascript-form-validation.php
Redirecting requests to other sources
Page 12Classification: Restricted
12
Redirecting Requests to Other Resources
• Servlet RedirectServlet
• Redirects the request to a different resource
• Why not just have links to those resources?
• Could determine browser type and choose link accordingly
• Could add parameters before redirecting (those sent originally are
included automatically)
• response.sendRedirect(URL);
• redirects to that URL
• immediately terminates current program
• Use relative paths whenever possible to make Website portable
Page 13Classification: Restricted
RedirectServlet redirecting requests to other resources.
1 // RedirectServlet.java
2 // Redirecting a user to a different Web page.
3
4 import javax.servlet.*;
5 import javax.servlet.http.*;
6 import java.io.*;
7
8 public class RedirectServlet extends HttpServlet {
9
10 // process "get" request from client
11 protected void doGet( HttpServletRequest request,
12 HttpServletResponse response )
13 throws ServletException, IOException
14 {
15 String location = request.getParameter( "page" );
16
17 if ( location != null )
18
19 if ( location.equals( "deitel" ) )
20 response.sendRedirect( "http://www.deitel.com" );
21 else
22 if ( location.equals( "welcome1" ) )
23 response.sendRedirect( "welcome1" );
24
13
Obtains the page parameter from the
request.
Redirects the request to
www.deitel.com.
Redirects the request to the servlet
WelcomeServlet.
Page 14Classification: Restricted
CRUD…
• Select
• Update
• Insert
• Delete
• 1. /Hello?op=Insert OR /Hello?op=Delete
• 2. /Hello/Insert OR /Hello/Delete
Page 15Classification: Restricted
RequestDispatcher forward()
RequestDispatcher rd=request.getRequestDispatcher(“<another servlet>");
rd.forward(request, response);
Page 16Classification: Restricted
RequestDispatcher include()
out.print(“<Your message to include goes here>");
RequestDispatcher rd=request.getRequestDispatcher(“nameOfHTMLorJSP");
rd.include(request, response);
Page 17Classification: Restricted
Request Dispatcher Demo
Page 18Classification: Restricted
Topics to be covered in next session
• Validation of Web Applications
• Redirecting requests
• Understanding Scope: parameters, attributes
Page 19Classification: Restricted
Thank you!

Session 28 - Servlets - Part 4

  • 1.
    Java & JEETraining Session 28 – Servlets - Part 4
  • 2.
    Page 1Classification: Restricted Agenda… •SQL Injection continued… + Exercises • Validating forms • Redirecting requests to other sources
  • 3.
    Page 2Classification: Restricted Reviewof previous session… ….
  • 4.
    Page 3Classification: Restricted WebServer and Application Server Presentation Tier: JSP, Servlets Run on Web Server or container JDBC on web server or app server Database Client: HTML JS CSS (Run on client machine on the browser)
  • 5.
    Page 4Classification: Restricted Demoof complete Web app flow… • Form which takes two fields… Student ID and Student Name… • Output should show whether that row exists or not, and if it exists, how many records in the table match the criteria (id, name) • If match found, then print login successful, else login failed.
  • 6.
  • 7.
    Page 6Classification: Restricted Validatingforms using JavaScript.. Best practice • MUST always validate both on client side as well as server side. • Validation in the client side is done using JavaScript. • Validation in the server side is done using Java. • Demo of frontend and backend validation. • Example of JS validation. • Check if value entered is a number or not: if (data === parseInt(data, 10)) alert("data is integer") else alert("data is not an integer");
  • 8.
    Page 7Classification: Restricted MoreJS Validation Examples…. • Let us look at some validation examples…
  • 9.
    Page 8Classification: Restricted Checkfor non-empty field… // If the length of the element's string is 0 then display helper message function required(inputtx) { if (inputtx.value.length == 0) { alert("message"); return false; } return true; }
  • 10.
    Page 9Classification: Restricted Checkfor restricting length of input field… function lengthRange(inputtxt, minlength, maxlength) { var userInput = inputtxt.value; if(userInput.length >= minlength && userInput.length <= maxlength){ return true; } else{ alert("Please input between " +minlength+ " and " +maxlength+ " characters"); return false; } }
  • 11.
    Page 10Classification: Restricted Referencefor more validation examples… • A good reference for form validation.. http://www.w3resource.com/javascript/form/javascript-form-validation.php
  • 12.
  • 13.
    Page 12Classification: Restricted 12 RedirectingRequests to Other Resources • Servlet RedirectServlet • Redirects the request to a different resource • Why not just have links to those resources? • Could determine browser type and choose link accordingly • Could add parameters before redirecting (those sent originally are included automatically) • response.sendRedirect(URL); • redirects to that URL • immediately terminates current program • Use relative paths whenever possible to make Website portable
  • 14.
    Page 13Classification: Restricted RedirectServletredirecting requests to other resources. 1 // RedirectServlet.java 2 // Redirecting a user to a different Web page. 3 4 import javax.servlet.*; 5 import javax.servlet.http.*; 6 import java.io.*; 7 8 public class RedirectServlet extends HttpServlet { 9 10 // process "get" request from client 11 protected void doGet( HttpServletRequest request, 12 HttpServletResponse response ) 13 throws ServletException, IOException 14 { 15 String location = request.getParameter( "page" ); 16 17 if ( location != null ) 18 19 if ( location.equals( "deitel" ) ) 20 response.sendRedirect( "http://www.deitel.com" ); 21 else 22 if ( location.equals( "welcome1" ) ) 23 response.sendRedirect( "welcome1" ); 24 13 Obtains the page parameter from the request. Redirects the request to www.deitel.com. Redirects the request to the servlet WelcomeServlet.
  • 15.
    Page 14Classification: Restricted CRUD… •Select • Update • Insert • Delete • 1. /Hello?op=Insert OR /Hello?op=Delete • 2. /Hello/Insert OR /Hello/Delete
  • 16.
    Page 15Classification: Restricted RequestDispatcherforward() RequestDispatcher rd=request.getRequestDispatcher(“<another servlet>"); rd.forward(request, response);
  • 17.
    Page 16Classification: Restricted RequestDispatcherinclude() out.print(“<Your message to include goes here>"); RequestDispatcher rd=request.getRequestDispatcher(“nameOfHTMLorJSP"); rd.include(request, response);
  • 18.
  • 19.
    Page 18Classification: Restricted Topicsto be covered in next session • Validation of Web Applications • Redirecting requests • Understanding Scope: parameters, attributes
  • 20.