Saturday, 9 May 2015

OCEJWCD - 3. Implementing an MVC Design (part1)

OCEJWCD (SCWCD) - 1Z0-899 - Web Component Developer Certification

In this post we are going to implement a simple application step by step:

The behaviour will be:
  1. Start the application (start.jsp).Introduce data in input fields and click the submit button.
  2. The servlet Login1 will be called.
  3. Welcome page (welcome.jsp) will be displayed with the name of the user.
Deployment descriptor:

<?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" 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>ExampleServlets</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>Login</servlet-name>
<servlet-class>example.Login1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
</web-app>
view raw web.xml hosted with ❤ by GitHub
Note: When creating a project in Eclipse use DynamicWeb Project. 

3.1. Code a controller using a servlet

The servlet code is created under src folder.

package example;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Login1
*/
public class Login1 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public Login1() {
super();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.getServletContext().getRequestDispatcher("/welcome.jsp").
forward(request, response);
}
}
view raw Login1.java hosted with ❤ by GitHub
3.2.  Code a view using a JSP

The JSPs have been created directly under WebContent.

start.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> JavaWithBreakfast Blog </title> </head>
<body>
<form action="login.do" method="post">
User:<input type="text" name="user"/><br/><br/>
Password:<input type="password" name="password"/><br/><br/>
<input type="submit" value="login"/>
</form>
</body>
</html>
view raw start.jsp hosted with ❤ by GitHub
welcome.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>JavaWithBreakfast Blog</title>
</head>
<body>
<h3> Welcome <%= request.getParameter("user") %> !</h3>
<p>The date is <%= new java.util.Date() %></p>
</body>
</html>
view raw welcome.jsp hosted with ❤ by GitHub

Executing the application:
-Start the application : http://localhost:8080/ExampleServlets/start.jsp



OCEJWCD (SCWCD) - 1Z0-899 - Web Component Developer Certification

No comments:

Post a Comment