00:00
/* SR00 - servelt + jsp theory */
/*
jsp and servelet
1. a protocal independent servelet - can handle different network protocal , such as HTTP, HTTPS, FTP, and more
2. GET = is used to retreive information form the given server using a give url, only retreive data
3. HEAD = same as get but transfer the status line and header section only
4. POST = use to send data to the server , customer information, file upload
5. PUT = replace all the current represation of the target resource with the upload content.
6. DELETE = remove all the current represtation of the target resource given by a URI.
web.xml file different task
1. servelet configuration
2. JSP file configuration
3. Filter configuration
4. Lintender configuration
5. Error page configuration
6. Welcome file configuration
*/
/* SR02 - how to maka and run the program in NetBeans */
/*
how to maka and run the program in NetBeans
1. RC > new Project > Java With Ant > java Web > Web Application > praject name > "Rajat" > Finished
2. click > rajat > Source Packages > click > Servelet
3. class name = "Raman" and package name ="bro"
4. check > add information to deployment descriptor ( web.xml
5. click > finished
*/
/*
web app settings step
1. open tag = <servlet-mapping>
2. <url-pattern> = type any name
3. <servlet-name> = type your desired name
4. open tag = <servlet>
5. <servlet-name> = this must be same
6. <servlet-class> = class with package
run
1. rc on java file run
2. command write /aaa (url - pattern), enter . can also be hit by any other html file link
*/
/* FirstServelet.java */
package Fser;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class FirstServelet extends HttpServlet {
// default request is get so use doGet method
@Override
protected void doGet(HttpServletRequest requset, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.print("i am in doGet method");
System.out.println("i ama in doGet() method for console");
}
}
/* index.html*/
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>this is rajat old laptop</h1>
<div>TODO write content:rajat singhvcc</div>
</body>
</html>
/* web.xml*/
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="6.0"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:
xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd">
<servlet>
<servlet-name>Firstlook</servlet-name>
<servlet-class>Fser.FirstServelet</servlet-class> // 2 this page will opem
</servlet>
<servlet-mapping>
<servlet-name>Firstlook</servlet-name>
<url-pattern>/aaa</url-pattern>// 1 hit this url
</servlet-mapping>
</web-app>
/* SR04 - Viper + Rattle + index + web */
/* Viper.java */
package snake.city;
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class Viper extends HttpServlet {
@Override
protected void service(HttpServletRequest requset, HttpServletResponse response) throws ServletException, IOException {
System.out.println("i am in viper method . . . . . .....");
}
}
/* Rattle.java */
package snake.city;
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class Rattle extends HttpServlet {
@Override
protected void service(HttpServletRequest requset, HttpServletResponse response) throws ServletException, IOException {
System.out.println("i am in rattle method . . . . . .....");
}
}
/* index.html */
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<a href = "VipSnake">viper snake </a></br>
<a href = "RatSnake">rattle snake </a></br>
</body>
</html>
/* web.xml */
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="6.0"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd">
<servlet>
<servlet-name>Viper</servlet-name>
<servlet-class>snake.city.Viper</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Viper</servlet-name>
<url-pattern>/VipSnake</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Rattle</servlet-name>
<servlet-class>snake.city.Rattle</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Rattle</servlet-name>
<url-pattern>/RatSnake</url-pattern>
</servlet-mapping>
</web-app>
/* SR05 - login.java + index.html + web.xml*/
/* login.java */
package com.rajat.in;
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class Login extends HttpServlet {
@Override
protected void service(HttpServletRequest requset, HttpServletResponse response) throws ServletException, IOException {
System.out.println("i am in service method . . . . . .....");
}
}
/* index.html*/
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<a href = "mylogin">Click me </a>
</body>
</html>
/* web.xml */
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="6.0"
xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd">
<servlet>
<servlet-name>Wxdemo</servlet-name>
<servlet-class>com.rajat.in.Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Wxdemo</servlet-name>
<url-pattern>/mylogin</url-pattern>
</servlet-mapping>
</web-app>
/* SR07 - servelet file (get the iput form user and print it ) */
package mouse;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet(name = "NewServlet", urlPatterns = {"/aaa"})
public class NewServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String myname = req.getParameter("name1");
String myemail = req.getParameter("email1");
System.out.println("Name 1: "+myname);
System.out.println("Email 1: "+myemail);
PrintWriter out = resp.getWriter();
out.println("Name : "+myname);
out.println("Email : "+myemail);
System.out.println(" get and post method . . . . . .....");
}
}
/* index.html */
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
<form action="aaa" method="post">
<input type="text" name="name1" /> <br/> <br/>
<input type="text" name="email1" /> <br/> <br/>
<input type="submit" value="Click Me" />
</form>
</body>
</html>
/* SR08 - servelet file using doget and dopost http methods*/
package web4;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet(name = "NewServlet4", urlPatterns = {"/myLogin"})
public class NewServlet4 extends HttpServlet {
@Override
// can use below mt instead of service mt
// doPost = for method type = POST
// doGet = for method type = GET
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String myemail = (String) req.getParameter("name1");
String mypass = (String) req.getParameter("pass1");
if(myemail.equals("raj@gmail.com") && mypass.equals("raj123")) {
System.out.println("success");
}
else {
System.out.println("failed");
}
}
}
/* index.html */
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h3> Login Form </h3>
<!-- <form action="myLogin" > defautl = get -->
<form action="myLogin" method="post">
<input type="text" name="name1" placeholder="Enter Name" /> <br/> <br/>
<input type="text" name="pass1" placeholder="Enter Password" /> <br/> <br/>
<input type="submit" value="Login" />
</form>
</body>
</html>
/* SR09 - servelet file using sendRedirect method */
package web5;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet(name = "Searchme", urlPatterns = {"/mysearch"})
public class Searchme extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
String mylook = req.getParameter("name1");
// resp.sendRedirect("https://www.google.com/");
resp.sendRedirect("https://www.google.com/search?q="+mylook);
}
}
/* index. html */
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h3> Login Form </h3>
<form action="mysearch" method="post">
<input type="text" name="name1" placeholder="Enter Name" /> <br/>
<input type="submit" value="search" />
</form>
</body>
</html>
/* SR10 - servelet file using request dispatcher interface */
package web6;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
@WebServlet(name = "NewServlet6", urlPatterns = {"/loogin"})
public class NewServlet6 extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String myemail = (String) req.getParameter("name1");
PrintWriter out = resp.getWriter();
if(myemail.equals("rajat")){
req.setAttribute("name_key","rajat"); // for profile page usually get form DB
RequestDispatcher rd = req.getRequestDispatcher("/profile.jsp");
rd.forward(req,resp);
}
else{
resp.setContentType("text/html"); // hide html code
out.print("<h3 style = 'color:red'>name not match</h3>");
RequestDispatcher rd = req.getRequestDispatcher("/index.html");
// rd.forward(req,resp); // only request
rd.include(req,resp); // add h3 tag also
}
}
}
/* index.html */
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h3> Login Form </h3>
<form action="loogin" method="post">
<input type="text" name="name1" placeholder="Enter Name" /> <br/>
<input type="submit" value="search" />
</form>
</body>
</html>
/* profile.jsp */
<%--
Document : profile
Created on : May 10, 2024, 5:33:50 PM
Author : rajat pc win8
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String myname = (String) request.getAttribute("name_key");
%>
<h1>welcome----- - <%= myname %></h1>
</body>
</html>
/* servelet file : session */
package web7;
import jakarta.servlet.RequestDispatcher;
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
@WebServlet(name = "NewServlet7", urlPatterns = {"/NewServlet7"})
public class NewServlet7 extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String myemail = (String) req.getParameter("name1");
PrintWriter out = resp.getWriter();
if(myemail.equals("rajat")) {
//req.setAttribute("name_key","rajat");
HttpSession session= req.getSession(); // session step-1
session.setAttribute("name_key", "Rajat ");
RequestDispatcher rd = req.getRequestDispatcher("/profile.jsp");
rd.forward(req,resp);
}
else{
resp.setContentType("text/html"); // hide html code
out.print("<h3 style = 'color:red'>name not match</h3>");
RequestDispatcher rd = req.getRequestDispatcher("/index.html");
// rd.forward(req,resp); // only request
rd.include(req,resp); // add h3 tag also
}
}
}
/* logout */
package web7;
import jakarta.servlet.RequestDispatcher;
import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
@WebServlet(name = "Logout", urlPatterns = {"/Logout"})
public class Logout extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
HttpSession session= req.getSession();
session.invalidate();
RequestDispatcher rd = req.getRequestDispatcher("/index.html");
rd.forward(req, resp);
}
}
/* about-us */
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%
// String myname = (String) request.getAttribute("name_key");
String myname = (String) session.getAttribute("name_key");
%>
<h1>welcome ------ <%= myname %></h1>
<a href="home.jsp"> Home </a>
<a href="profile.jsp"> profile</a>
<a href="Logout"> Logout </a>
</body>
</html>
/* home.jsp */
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%
// String myname = (String) request.getAttribute("name_key");
String myname = (String) session.getAttribute("name_key");
%>
<h1>welcome - - - - <%= myname %></h1>
<a href="about-us.jsp"> About Us </a>
<a href="profile.jsp"> profile</a>
<a href="Logout"> Logout </a>
</body>
</html>
/* index.html */
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h3> Login Form </h3>
<form action="NewServlet7" method="post">
<input type="text" name="name1" placeholder="Enter Name" /> <br/>
<input type="submit" value="search" />
</form>
</body>
</html>
/* profile.jsp */
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
// String myname = (String) request.getAttribute("name_key");
String myname = (String) session.getAttribute("name_key");
%>
<h1>welcome - - - - -<%= myname %></h1>
<a href="home.jsp"> Home </a>
<a href="about-us.jsp"> About Us </a>
<a href="Logout"> Logout </a>
</body>
</html>
/* SR16 - jsp basic code */
<%--
Document : index
Created on : May 11, 2024, 9:38:23 AM
Author : rajat pc win8
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<h3> Welcome to Smart Programming </h3>
<%
String name = "rajat singh negi";
int leng = name.length();
%>
<%= leng %>
</body>
</html>
/* SR18 - jsp more code */
/* index.jsp */
<%--
Document : index
Created on : May 11, 2024, 9:51:04 AM
Author : rajat pc win8
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%!
int a=10;
String name = "deepak";
int square(){
return a*a;
}
%>
<%
out.println("a: "+a);
out.println("Name : "+name);
out.println(square());
int b=20;
if(b<100) {
out.println("b is smaller than 100");
}
else {
out.println("b is greater then 100");
}
for(int i=1; i<=5; i++) {
out.println(i);
}
//String name= request.getParameter("----");
%>
<%= a %>
<%= name %>
<%= square() %>
<%= Math.random() %>
</body>
</html>
/* SR20 - JSP implicit objects code */
// SR20a.png
// SR20b.png
/* index.jsp*/
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String name = "Rajat singh";
out.println(name);
%>
<%
session.setAttribute("session_name", "dehradoon city");
%>
<form action="output.jsp" method="get">
<input type="text" name="name1" placeholder="Enter Name" />
<input type="submit" value="Click Me" />
</form>
</body>
</html>
/* output.jsp */
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%-- using out, request, session directly , implicitly- - - %>
<%
String myname= request.getParameter("name1");
out.println(myname);
%>
<%
String cname = (String) session.getAttribute("session_name");
out.println(cname);
%>
</body>
</html>
/* SR22 - Expression Language, ${-} , code */
/* index.jsp */
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
request.setAttribute("request_name", "Rajat Singh");
//out.println(request.getAttribute("request_name"));
%>
<h3> Hello : ${requestScope.request_name} </h3>
<%
session.setAttribute("session_cname", "TATTO HEAD");
//out.println(session.getAttribute("session_cname"));
%>
<h3> Company Name: ${session_cname} </h3>
<form action="output.jsp" method="get">
<input type="text" name="name1" placeholder="Enter Name" />
<input type="submit" value="Click Me" />
</form>
</body>
</html>
/* output.jsp */
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String myname = request.getParameter("name1");
out.println(myname);
%>
${param.name1}
${10+25}
</body>
</html>