HTML – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Mon, 04 Jul 2022 06:33:50 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg HTML – Programmerbay https://programmerbay.com 32 32 Create an application to add and multiply two numbers to the servlet to the user in apache tomcat server https://programmerbay.com/create-an-application-to-add-and-multiply-two-numbers-to-the-servlet-to-the-user-in-apache-tomcat-server/ https://programmerbay.com/create-an-application-to-add-and-multiply-two-numbers-to-the-servlet-to-the-user-in-apache-tomcat-server/#respond Sat, 07 Nov 2020 14:34:36 +0000 https://programmerbay.com/?p=5656 We are assuming that you have already downloaded and setup the Eclipse Environment and Tomcat server.

Here are the steps and the code to add and multiply two numbers to servlet in apache tomcat server

1) On Eclipse, create a new project and select Dynamic Web Project by ticking on producing deployment descriptor file.

2. Firstly hover on your newly created project, right-click on it and tab on new and select HTML file. ( or anything you want).

Index.html

<!DOCTYPE html>
<html>
<head>
<title>Practical Work</title>
</head>
<body>
<form action="add_me">
<label>First number </label> <input type="text" name="num1"/> <br/><br/>
<label>Second number </label> <input type="text" name="num2"/> <br/><br/>
<button type="submit" name="calculate">Product and Sum </button><br/>
</form>
</body>
</html>

3. Now, again hover on the project , right click on it and select new and tab on class that would create a java file.

Create a servlet class.

Add_Numbers.java

package com.programmerbay;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.*;
public class Add_Numbers extends HttpServlet{
public void service(HttpServletRequest request,HttpServletResponse response) throws IOException
{
int num1 = Integer.parseInt(request.getParameter("num1"));
int num2 = Integer.parseInt(request.getParameter("num2"));
int sum = num1 + num2;
int product = num1 * num2;
PrintWriter output = response.getWriter();
output.println("The Answer :"+sum +"\n The product :"+product);
}
}

4. Configure the Web.xml file which you find in Web Content folder.

web.xml

<?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">

<servlet>
<servlet-name>Add</servlet-name>
<servlet-class>com.programmerbay.Add_Numbers</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Add</servlet-name>
<url-pattern>/add_me</url-pattern>

</servlet-mapping>
</web-app>

5. Open the previously created index.html and run it by selecting the “restart server”.

Output:

Servlet java code to add two numbers

Servlet java code to add two numbers 2

]]>
https://programmerbay.com/create-an-application-to-add-and-multiply-two-numbers-to-the-servlet-to-the-user-in-apache-tomcat-server/feed/ 0
Create an application to display current date and time in apache tomcat server https://programmerbay.com/create-an-application-to-display-current-date-and-time-in-apache-tomcat-server/ https://programmerbay.com/create-an-application-to-display-current-date-and-time-in-apache-tomcat-server/#respond Sun, 08 Dec 2019 14:06:53 +0000 https://programmerbay.com/?p=5669 Here is the code to display current date and time in the apache tomcat server.

  1. Create a landing page.

Index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Practical Work</title>
</head>
<body>
<form action="perform_something.jsp">

  <button type="submit">Click here to know current date and time</button><br/> </form>
</body>
</html

2. To show the current date and time, use Java date object in a JSP page.

Perform_Something.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.util.Date"%>
<!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>Insert title here</title>
</head>
<body>
<%
Date da = new Date();

%>
Date:<%= da.toLocaleString() %>
</body>
</html>

Output:

 display current date in jsp

 

 

display current date in jsp 1

]]>
https://programmerbay.com/create-an-application-to-display-current-date-and-time-in-apache-tomcat-server/feed/ 0
Create an application with two textboxes username and password on receiving the request to the user details and display welcome username on another page https://programmerbay.com/create-an-application-with-two-textboxes-username-and-password-on-receiving-the-request-to-the-user-details-and-display-welcome-username-on-another-page/ https://programmerbay.com/create-an-application-with-two-textboxes-username-and-password-on-receiving-the-request-to-the-user-details-and-display-welcome-username-on-another-page/#respond Sun, 08 Dec 2019 13:55:58 +0000 https://programmerbay.com/?p=5673 In this article, we’re going to look at a basic login verification form using JSP and servlet. If a user’s entered username and password matches with original username and password then ‘welcome’ page would open.

Here’s the code with two textboxes username and password on receiving the request to verify the user details and display welcome username on another page.

1.Make a landing page in JSP where two text boxes and a submit button will be displayed.

index.jsp

<%@ 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>Practical Work</title>
</head>
<body>
<% 
String msg = (String) request.getAttribute("message"); 
if(msg==null)
  out.println("\t");
else
out.println(msg);
%>
<form action="login">
<label>Username : </label> <input type="text" name="username"/>
<label>Password : </label> <input type="password" name="pass"/>
  <button type="submit">Login</button><br/> </form>
</body>
</html>

2. Now, it’s time to verify the user which would be done by servlet at backend.

login.java

package com.programmerbay;

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

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class login extends HttpServlet{
  public void service(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException
  {
    
  String username = request.getParameter("username")	;
  String password = request.getParameter("pass")	;
    if(username.equals("programmerbay") && password.equals("12345"))
  {
HttpSession session = request.getSession();
session.setAttribute("username", username);
  response.sendRedirect("home.jsp");
  }
  else
  {
    String msg;
    if(username.isEmpty() || password.isEmpty())
    msg ="Fields are  empty";
    else
    msg ="Password is incorrect!!";
    request.setAttribute("message", msg);
        
    RequestDispatcher redirect = request.getRequestDispatcher("index.jsp");
    redirect.forward(request,response);
  }
  }

}

3. If a user type in correct username and password, ‘welcome’ page would be displayed.

Home.jsp

<%@ 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>Home</title>
</head>
<body>
<%
String username = (String)session.getAttribute("username");
if(username==null)
{
  response.sendRedirect("index.html");
}
%>

<h1>This is home Screen</h1>
<form action="logout" action="get">
 <button type="submit">Logout</button>
  </form>
</body>
</html>

4. To end the session, a logout servlet will work whenever a user clicks on the logout button.

logout.java

package com.programmerbay;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class logout extends HttpServlet{
  public void service(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException
  {
    HttpSession session = request.getSession();
    session.removeAttribute("username");
    session.invalidate();
    response.sendRedirect("index.jsp");
  }
}

Output:

Login page

login page

When either username or password is not entered

fields are empty

When a user enters a wrong password or username

password incorrect

Homepage

home

]]>
https://programmerbay.com/create-an-application-with-two-textboxes-username-and-password-on-receiving-the-request-to-the-user-details-and-display-welcome-username-on-another-page/feed/ 0
Create an application to display addition and product of two numbers on the client side using JSP https://programmerbay.com/create-an-application-to-display-addition-and-product-of-two-numbers-on-the-client-side-using-jsp/ Sat, 07 Dec 2019 15:21:11 +0000 https://programmerbay.com/?p=5667 Simple code to display addition and product of two numbers using JSP.
1) Create a landing page in HTML.
Index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Practical Work</title>
</head>
<body>
<form action="add_multiply.jsp">
<label>First number </label>
 <input type="text" name="num1"/> <br/><br/>
  <label>Second number </label> <input type="text" name="num2"/> <br/><br/> 
  <button type="submit" name="calculate">Product and Sum </button><br/> </form>
</body>
</html>

2) Make a module in JSP that will print the sum and product of two numbers
add_multiply.jsp

<%@ 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>Insert title here</title>
</head>
<body>
<%
int num1 = Integer.parseInt(request.getParameter("num1"));
int num2 = Integer.parseInt(request.getParameter("num2"));
int sum = num1 + num2;
int product = num1 * num2;
out.println("This is the sum = "+sum+"\n This is the product = "+product);
%>
</body>

Output:

Servlet java code to add two numbers 2

]]>
Create an application to call a servlet from another servlet using RequestDispatcher in apache tomcat server https://programmerbay.com/create-an-application-to-call-a-servlet-from-another-servlet-in-apache-tomcat-server/ https://programmerbay.com/create-an-application-to-call-a-servlet-from-another-servlet-in-apache-tomcat-server/#respond Sat, 07 Dec 2019 15:10:49 +0000 https://programmerbay.com/?p=5660 Here’s the code to invoke a servlet from another servlet using RequestDispatcher interface.
1) Create index.html.

Index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Practical Work</title>
</head>
<body>
<form action="first_servlet">
WRITE <input type="text" name="word" value="I am calling first servlet"/>
<button type="submit" name="calculate">Call First Servlet </button><br/>
</form>
</body>
</html>

2) Now, create a basic servlet and pass the data of textbox field from index.html to First_Servlet.java. Use requestdispatcher to rely the data to other servlet.

First_Servlet.java

package com.programmerbay;

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

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

public class First_Servlet extends HttpServlet{
public void service(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException
{
String word = request.getParameter("word") ;
PrintWriter output = response.getWriter();

String word1 = word+".\n Now, It is calling second servlet";
// Dispatching it to second servlet
RequestDispatcher redirecting = request.getRequestDispatcher("second_servlet");
request.setAttribute("passingWord", word1);
redirecting.forward(request, response);
}

}

3) After that, write the code for second servlet that will handle the message or information from first servlet.

Second_Servlet.java

package com.programmerbay;

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

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Second_Servlet extends HttpServlet{
public void service(HttpServletRequest request,HttpServletResponse response) throws IOException
{
String word = (String) request.getAttribute("passingWord");
PrintWriter output = response.getWriter();
output.println(word);

}
}

4) Edit the deployment descriptor.

Web.xml

<?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">

<servlet>
<servlet-name>Invoke_First</servlet-name>
<servlet-class>com.programmerbay.First_Servlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Invoke_First</servlet-name>
<url-pattern>/first_servlet</url-pattern>

</servlet-mapping>

<servlet>
<servlet-name>Invoke_Second</servlet-name>
<servlet-class>com.programmerbay.Second_Servlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Invoke_Second</servlet-name>
<url-pattern>/second_servlet</url-pattern>

</servlet-mapping>


</web-app>

Output:

call one servlet from another

]]>
https://programmerbay.com/create-an-application-to-call-a-servlet-from-another-servlet-in-apache-tomcat-server/feed/ 0
Difference between HTML and XML https://programmerbay.com/difference-between-html-and-xml/ https://programmerbay.com/difference-between-html-and-xml/#respond Sun, 01 Sep 2019 09:00:08 +0000 https://www.programmerbay.com/?p=4874 HTML and XML both have different purposes to serve, one is used for presenting  data and other one is used for transferring data. Lets see how these two are different from each other.

Difference between HTML and XML in Tabular form

HTMLXML
Stands for Hypertext mark–up LanguageStands for eXtensible mark–up Language
It is a predefined languageIt provides a credible framework for defining the mark – up language
It does not contain any kind of structural informationIt contains the structural information
The purpose of this mark–up language is to effectively represent the dataThe purpose of this mark–up language is to have an efficient transfer of information
No preservation of the white spacesWhite spaces can be preserved
It allows to have both paired and non-paired tagsIt allows only paired tags
It is not case sensitiveIt is case sensitive

HTML

The mark–up language which is used for creating webpages is called HTML. It employs TAGS which help in constructing and presenting the layout of the webpage. All the HTML elements are evaluated and run on the browser and present it over the screen.

XML

The mark – up language which helps the user to calibrate data representation or the structure of data in which each and every detail is assigned. This XML mark–up language can be used to create user-defined tags and user-defined mark–up language. The XML focuses on the fact that how the data is stored.

Key Differences

  • FULL FORM

HTML stands for Hypertext mark–up Language; whereas; XML stands for eXtensible mark–up Language.

  • GENERAL

HTML language is an already predefined language; whereas; when we talk about the XML language then, the XML language provides a credible framework for defining the mark – up languages.

  • STRUCTURAL INFORMATION

In case of the HTML language, this mark – up language does not contain any kind of structural information, whereas; in case of XML language then, this mark – up language always contain the structural information.

  • LANGUAGE PURPOSE

When we consider the HTML language, the purpose of this mark–up language is to effectively represent the data; whereas; when we consider the XML language, the purpose of this mark–up language is to have an efficient transfer of information.

  • WHITE SPACE PRESERVATION

In the HTML language there is no preservation of the white spaces as such; whereas; when we talk about the XML language then, in the XML language the white spaces can be preserved.

  • CLOSING TAGS

In case of the HTML language, it has an optional approach towards them as, some tags can also work without closing tags; whereas; in case of the XML language, it has a compulsory approach towards them as every tag is required to have a closing tag.

  • NESTING

When we consider the HTML language then, the nesting has no valuable effect on the HTML code, so there is no need for it; whereas; when we consider the XML language then, the nesting has a great value in the XML code, so it must be properly done.

]]>
https://programmerbay.com/difference-between-html-and-xml/feed/ 0