EX.
NO: 13
DATE:
IMPLEMENTING MVC WITH REQUEST DISPATCHER
AIM:
Develop Login application to implement MVC with Request Dispatcher.
PROCEDURE:
1. Create a Java EE Web Project Select Java Web > Web Application and click Next.
Choose a Project Name (e.g. EmailLogin) and click Next.
2. Add a JSP Page Open the [New JSP File] dialog box by right clicking the Web Pages node (in the
[Projects] window) and selecting New > JSP... for obtaining email, and password and clicks on submit
then the action is passed in mvc_servlet where email and password are passed.
3. Add a Servlet Class Open the [New Servlet] dialog box selecting New > Servlet... (e.g. mvc_servlet is
controller layer, the request is sent to the bean object which act as model layer.
4. Create a java bean (e.g LoginBean). The email and password values are set into the beanand stored for
further purpose.
5. Add a another JSP Page Open the [New JSP File] dialog box by right clicking the Web Pages node (in
the [Projects] window) and selecting New > JSP(e.g [Link] ) From the bean,the value is fetched and
shown in the view layer.
PROGRAM:
[Link]
<!DOCTYPE html>
<html>
<head>
<title>Start Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Hello World!</h1>
<form action="ControllerServlet" method="post">
Name:<input type="text" name="name"><br>
Password:<input type="password" name="password"><br>
<input type="submit" value="login">
</form></body>
</html>
[Link]
package [Link].mvc1;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class ControllerServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponseresponse) throws
ServletException, IOException {
[Link]("text/html");
PrintWriter out=[Link]();
String name=[Link]("name");
String password=[Link]("password");
LoginBean bean=new LoginBean();
[Link](name);
[Link](password);
[Link]("bean",bean);
boolean status=[Link]();
if(status){
RequestDispatcher rd=[Link]("[Link]");
[Link](request, response);
}
else{
RequestDispatcher rd=[Link]("[Link]");
[Link](request, response);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponseresp) throws ServletException,
IOException {
doPost(req, resp);
}}
[Link]
package [Link].mvc1;
public class LoginBean {
private String name,password;
publicString getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password)
{ [Link] = password;
}
public boolean validate(){
if([Link]("admin")){
return true;
}
else{
return false;
}} }
[Link]
<%@page import="[Link]"%>
<p>You are successfully logged in!</p>
<% LoginBean bean=(LoginBean)[Link]("bean");
[Link]("Welcome, "+[Link]());
%>
<h1>Hello World!</h1>
</body>
</html>
[Link]
<p>Sorry! username or password error</p>
<%@ include file="[Link]" %>
Output:
RESULT:
Thus the program has been executed successfully and output verified.
[Link]: 14
DATE:
TO BUILD A WEB APPLICATION USING STRUCTS
AIM:
To build a web application that collects the user’s name and displays “Hello World” followed by
the user name.
PROCEDURE:
1. Create the Model Class [Link]
2. Create the Action Class [Link]
3. Create the View [Link]
4. Add the Struts Configuration In [Link]
5. Create the URL Action
6. Build the WAR File and run the Application
PROGRAM:
[Link]
package [Link];
public class MessageStore {
private String message;
public MessageStore() {
message = "Hello Struts User";
}
public String getMessage()
{ return message;
}}
[Link]
package [Link];
import [Link];
import [Link];
public class HelloWorldAction extends ActionSupport {
private MessageStore messageStore;
public String execute() {
message Store = new MessageStore() ;
return SUCCESS;
}
public MessageStore getMessageStore()
{ return messageStore;
}}
[Link]
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World!</title>
</head>
<body>
<h2><s:property value="[Link]" /></h2>
</body>
</html>
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration2.5//EN"
"[Link]
<struts>
<constant name="[Link]" value="true" />
<package name="basicstruts2" extends="struts-default">
<action name="index">
<result>/[Link]</result>
</action>
<action name="hello" class="[Link]"
method="execute">
<result name="success">/[Link]</result>
</action>
</package>
</struts>
[Link]
<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Basic Struts 2 Application - Welcome</title>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<p><a href="<s:url action='hello'/>">Hello World</a></p>
</body>
</html>
OUTPUT:
RESULT:
Thus the program has been executed successfully and output verified.
[Link]: 15
DATE:
TO UPLOAD A SELECTED FILE USING STRUCTS
AIM:
Create our view which will be required to browse and upload a selected file.
PROCEDURE:
Step 1: Create an [Link] with plain HTML upload form that allows the user to upload a file
Step 2: Create a simple jsp file [Link] to display the outcome of our file upload in case it
becomessuccess.
Step 3: Create [Link] in case there is some error in uploading the file
Step 4: Create a Java class called [Link] which will take care of uploading file and storing
that file at a secure location
Step 5: Add The Struts Configuration in [Link]
Step 6: Add <interceptor-ref> tag inside <action> in [Link] 7: Create The URL Action
Step 8: Build the WAR File and Run The Application
PROGRAM:
[Link]
<%@ page language = "java" contentType = "text/html; charset = ISO-8859-1"pageEncoding =
"ISO-8859-1"%>
<%@ taglib prefix = "s" uri = "/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"[Link]
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form action = "upload" method = "post" enctype = "multipart/form-data">
<label for = "myFile">Upload your file</label>
<input type = "file" name = "myFile" />
<input type = "submit" value = "Upload"/>
</form>
</body>
</html>
[Link]
<%@ page contentType = "text/html; charset = UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<html>
<head>
<title>File Upload Success</title>
</head>
<body>
You have successfully uploaded <s:property value = "myFileFileName"/>
</body>
</html>
[Link]
<%@ page contentType = "text/html; charset = UTF-8" %>
<%@ taglib prefix = "s" uri = "/struts-tags" %>
<html>
<head>
<title>File Upload Error</title>
</head>
<body>
There has been an error in uploading the file.
</body>
</html>
[Link]
package [Link].struts2;
import [Link];
import [Link];
import [Link];
import [Link];
public class uploadFile extends ActionSupport {
private File myFile;
private String myFileContentType;
private String myFileFileName;
private String destPath;
public String execute() {
/* Copy file to a safe location */
destPath = "C:/apache-tomcat-6.0.33/work/";
try {
[Link]("Src File name: " + myFile);
[Link]("Dst File name: " + myFileFileName);
File destFile = new File(destPath, myFileFileName);
[Link](myFile, destFile);
}
catch(IOException e) {
[Link]();
return ERROR;
}
return SUCCESS;
}
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
[Link] = myFile;
}
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String myFileContentType) {
[Link] = myFileContentType;
}
public String getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
[Link] = myFileFileName;
}}
[Link]
<?xml version = "1.0" Encoding = "UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"[Link]
<struts>
<constant name = "[Link]" value = "true" />
<constant name = "[Link]" value = "1000000" />
<package name = "helloworld" extends = "struts-default">
<action name = "upload" class = "[Link]">
<result name = "success">/[Link]</result>
<result name = "error">/[Link]</result>
</action>
</package>
</struts>
[Link]
<?xml version = "1.0" Encoding = "UTF-8"?>
<web-app xmlns:xsi = "[Link] xmlns =
"[Link]
xmlns:web = "[Link] =
"[Link] [Link]
id = "WebApp_ID" version = "3.0">
<display-name>Struts 2</display-name>
<welcome-file-list>
<welcome-file>[Link]</welcome-file>
</welcome-file-list>
<filter>
<filter-name>struts2</filter-name>
<filter-class> [Link]
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
OUTPUT:
RESULT:
Thus the program has been executed successfully and output verified.