Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

Servlet Tutorial: Getting Starting with JSP – Servlet Example

$
0
0

Simple JSP Servlet Example Servlet Tutorial: Getting Starting with JSP   Servlet Example

Here is a simple JSP – Servlet example with step-by-step instructions. I’ll demonstrate how to retrieve request parameters in JSP – Servlet example.

Here are the steps we are going to perform:

Here is a complete project structure:

Crunchify JSP Servlet Example Servlet Tutorial: Getting Starting with JSP   Servlet Example

Step1: Create HelloCrunchify.java file

package com.crunchify.jsp.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author Crunchify.com
 */

public class HelloCrunchify extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// reading the user input
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		PrintWriter out = response.getWriter();
		out.println (
			      "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" +" +
			          "http://www.w3.org/TR/html4/loose.dtd\">\n" +
			      "<html> \n" +
			        "<head> \n" +
			          "<meta http-equiv=\"Content-Type\" content=\"text/html; " +
			            "charset=ISO-8859-1\"> \n" +
			          "<title> Crunchify.com JSP Servlet Example  </title> \n" +
			        "</head> \n" +
			        "<body> <div align='center'> \n" +
			          "<style= \"font-size=\"12px\" color='black'\"" + "\">" +
			            "Username: " + username + " <br> " + 
			            "Password: " + password +
			        "</font></body> \n" +
			      "</html>" 
			    );  	
		}
}

Another must read:

Step2: Create Crunchify.jsp file:

<%@ 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>
<style type="text/css">
body {
	background-image:
		url('http://cdn3.crunchify.com/wp-content/uploads/2013/03/Crunchify.bg_.300.png');
}
</style>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Crunchify JSP Servlet Example</title>
</head>
<body>

	<div align="center" style="margin-top: 50px;">

		<form action="CrunchifyServlet">
			Please enter your Username:  <input type="text" name="username" size="20px"> <br>
			Please enter your Password:  <input type="text" name="password" size="20px"> <br><br>
		<input type="submit" value="submit">
		</form>

	</div>

</body>
</html>

Step3: web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<display-name>CrunchifyJSPServletExample</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	<servlet>
		<servlet-name>Hello</servlet-name>
		<servlet-class>com.crunchify.jsp.servlet.HelloCrunchify</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>Hello</servlet-name>
		<url-pattern>/CrunchifyServlet</url-pattern>
	</servlet-mapping>
</web-app>

Step4: Deployed and Run CrunchifyJSPServletExample on Tomcat.

Run JSP Servlet Project on Tomcat Servlet Tutorial: Getting Starting with JSP   Servlet Example

Step5: Point your URL to http://localhost:8080/CrunchifyJSPServletExample/Crunchify.jsp

Step6: Checkout Result. (Your URL should look like this: http://localhost:8080/CrunchifyJSPServletExample/CrunchifyServlet?username=Crunchify&password=abcdefabc)

Checkout Result Crunchify JSP Servlet Servlet Tutorial: Getting Starting with JSP   Servlet Example

Note that the server does not recognize changes in “web.xml” except if you restarted, or started, the server.

The post Servlet Tutorial: Getting Starting with JSP – Servlet Example appeared first on Crunchify.


Viewing all articles
Browse latest Browse all 1037

Trending Articles