Friday, May 3, 2013

Sample Java MVC Web Application - Part I

Let us create a dynamic java web application in eclipse with 2 jsp files and 1 servlet. See project structure as -



No model at this stage and let us see the code -

index.jsp -
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
    <form action="servletcontroller" method="post">
        <table border="0">
            <tr>
                <td>First Name:</td>
                <td><input type="text" name="firstname" /></td>
            </tr>
            <tr>
                <td>Last Name:</td>
                <td><input type="text" name="lastname" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    </form>
</body>
</html>

output.jsp -
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Insert title here</title>
</head>
<body>
    <h1>Your first and last name is:</h1>

    <%
        String firstName = (String) request.getAttribute("firstname");
        String lastName = (String) request.getAttribute("lastname");
    %>

    <table>
        <tr>
            <td>First Name:</td>
            <td><%=firstName%></td>
        </tr>
        <tr>
            <td>Last Name:</td>
            <td><%=lastName%></td>
        </tr>
    </table>
</body>
</html>

ServletController.java -
package com.mqin.example;

import java.io.IOException;

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

public class ServletController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ServletController() {
        super();
    }

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

        if (request.getParameter("firstname") == null 
                || request.getParameter("lastname") == null
                || request.getParameter("firstname") == "" 
                || request.getParameter("lastname") == "") {
            getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
            return;
        }

        String firstName = request.getParameter("firstname");
        String lastName = request.getParameter("lastname");

        request.setAttribute("firstname", firstName);
        request.setAttribute("lastname", lastName);

        getServletContext().getRequestDispatcher("/output.jsp").forward(request, response);
    }

}


web.xml -
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>JavaMVC</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>
    <description></description>
    <display-name>ServletController</display-name>
    <servlet-name>ServletController</servlet-name>
    <servlet-class>com.mqin.example.ServletController</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletController</servlet-name>
    <url-pattern>/servletcontroller</url-pattern>
  </servlet-mapping>
</web-app>


Step 1, run index.jsp -











Step 2, click submit with empty value -











Step 3, input value -











Step 4, click submit - 











*Note* Good explanation about forward and redirect -
forward vs redirect
getServletContext().getRequestDispatcher("/output.jsp").forward(request, response);
response.sendRedirect(request.getContextPath() + "/output.jsp");

No comments:

Post a Comment