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:
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.
3.2. Code a view using a JSP
In this post we are going to implement a simple application step by step:
The behaviour will be:
- Start the application (start.jsp).Introduce data in input fields and click the submit button.
- The servlet Login1 will be called.
- Welcome page (welcome.jsp) will be displayed with the name of the user.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
3.1. Code a controller using a servlet
The servlet code is created under src folder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
The JSPs have been created directly under WebContent.
start.jsp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ 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> |
Executing the application:
-Start the application : http://localhost:8080/ExampleServlets/start.jsp
No comments:
Post a Comment