Unable to understand Struts

Unable to understand Struts

I am studying in GNIIT from NIIT.
Here in 4th sem we have stuts also but I am not able to understand it.
I've made an application.That application is of book entry.
I have made the application but in that application when I enter the details of book, data base becomes updated but I did'nt get successfull msg.
it doesnat forwads on the success page.



package com.struts.controller;


import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import servlet.model.Book;
import servlet.model.BookService;

/**
*
* @author niit
*/
public class AddBookStruts extends org.apache.struts.action.Action {

/* forward name="success" path="" */
private static final String SUCCESS = "success";

/**
* This is the action called from the Struts framework.
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
* @throws java.lang.Exception
* @return
*/
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception

{
PrintWriter out = response.getWriter();
try{

out.println("<a href='index.jsp'>Home Page</a>");
BookService bs=new BookService("C:\\data\\");

String strAuthor=request.getParameter("author");
String strPages=request.getParameter("pages");
String strDescription=request.getParameter("description");

int isbn=bs.AutoIncrement();
int pages=Integer.parseInt(strPages);

Book book=new Book(isbn,strAuthor,pages,strDescription);

bs.storeBook(book);

request.setAttribute("book",book);

return mapping.findForward(SUCCESS);
}

catch(Exception ex)
{
out.println(ex.toString());
}
finally
{

out.close();
}
return mapping.findForward("error");
}
}
---------------------------------------------------------------------------
package servlet.view;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import servlet.model.Book;
import servlet.model.BookService;

/**
*
* @author niit
*/
public class AddBookServlet extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
out.println("<a href='index.jsp'>Home Page</a>");

BookService bs=new BookService("C:\\data\\");

String strAuthor=request.getParameter("author");
String strPages=request.getParameter("pages");
String strDescription=request.getParameter("description");

int isbn=bs.AutoIncrement();
int pages=Integer.parseInt(strPages);

Book book=new Book(isbn,strAuthor,pages,strDescription);

bs.storeBook(book);

request.setAttribute("book",book);

return;

}
catch(Exception ex)
{
out.println(ex.toString());
}
finally {
out.close();
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

}
---------------------------------------------------------------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package servlet.view;

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

/**
*
* @author NIIT
*/
public class SuccessServlet extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Book book=(Book) request.getAttribute("book");
try {
// out.println("<a href='index.jsp'>Home Page</a>");
// out.println("<h1>Success</h1>");
// out.println("<p>");
// out.println("Your request to add the Book title");
// out.println("<font color=green>");
// out.println("<u>");
// out.print("<i>" + book.getDescription()+ "</i>");
// out.println("</u>");
// out.println("</font>");
// out.println("By");
// out.println("<font color=green>");
// out.println("<u>");
// out.print("<i>" + book.getAuthor()+ "</i>");
// out.println("</u>");
// out.println("</font>");
// out.println(" was successful.");
// out.println("</p>");
} finally {
out.close();
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

}
---------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">


<struts-config>
<form-beans>

</form-beans>

<global-exceptions>

</global-exceptions>

<global-forwards>
<forward name="welcome" path="/Welcome.do"/>
</global-forwards>

<action-mappings>
<action path="/Welcome" forward="/welcomeStruts.jsp"/>
<action path="/com.struts.controller" type="com.struts.controller.DeleteBookStruts">
<forward name="SuccessDelete" path="servlet.view"/>
<forward name="ErrorServlet" path="servlet.view"/>
</action>
<action path="/com.struts.controller" type="com.struts.controller.AddBookStruts">
<forward name="Success" path="/success.jsp"/>
<forward name="Error" path="/Error.jsp"/>
</action>

</action-mappings>


<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

<message-resources parameter="com/myapp/struts/ApplicationResource"/>

<!-- ========================= Tiles plugin ===============================-->
<!--
This plugin initialize Tiles definition factory. This later can takes some
parameters explained here after. The plugin first read parameters from
web.xml, thenoverload them with parameters defined here. All parameters
are optional.
The plugin should be declared in each struts-config file.
- definitions-config: (optional)
Specify configuration file names. There can be several comma
separated file names (default: ?? )
- moduleAware: (optional - struts1.1)
Specify if the Tiles definition factory is module aware. If true
(default), there will be one factory for each Struts module.
If false, there will be one common factory for all module. In this
later case, it is still needed to declare one plugin per module.
The factory will be initialized with parameters found in the first
initialized plugin (generally the one associated with the default
module).
true : One factory per module. (default)
false : one single shared factory for all modules
- definitions-parser-validate: (optional)
Specify if xml parser should validate the Tiles configuration file.
true : validate. DTD should be specified in file header (default)
false : no validation

Paths found in Tiles definitions are relative to the main context.
-->
<plug-in className="org.apache.struts.tiles.TilesPlugin" >
<set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" />
<set-property property="moduleAware" value="true" />
</plug-in>

<!-- ========================= Validator plugin ================================= -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

</struts-config>
---------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>AddBookServlet</servlet-name>
<servlet-class>servlet.view.AddBookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AddBookServlet</servlet-name>
<url-pattern>/add.view</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
---------------------------------------------------------------------------
<%--
Document : success
Created on : May 25, 2010, 11:56:30 AM
Author : NIIT
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Success Page</title>
</head>
<body>
<h1>Success!</h1>
<p>
Your request to add the Book title was successful.
</p>
</body>
</html>
---------------------------------------------------------------------------
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

<html:html lang="true">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><bean:message key="welcome.title"/></title>
<html:base/>
</head>
<body style="background-color: white">

<logic:notPresent name="org.apache.struts.action.MESSAGE" scope="application">
<div style="color: red">

</div>
</logic:notPresent>

<h3><bean:message key="welcome.heading"/></h3>
<p><bean:message key="welcome.message"/></p>
<a href="addbooks.jsp">Add Book</a>

</body>
</html:html>
---------------------------------------------------------------------------
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<jsp:forward page="Welcome.do"/>
View Answers









Related Tutorials/Questions & Answers:
Unable to understand Struts - Struts
Unable to understand Struts  I am studying in GNIIT from NIIT. Here in 4th sem we have stuts also but I am not able to understand it. I've made... = "success"; /** * This is the action called from the Struts
Form unable to hold values - Struts
Form unable to hold values  I have a form with a combo box having hasmap property. When i submit the search button and in event of an error, it does not bring the value only for this drop down box? Any ideas
Advertisements
unable to execute the examples
unable to execute the examples  unable to execute the examples given for struts,ejb,or spring tutorials.Please help I am a beginner
Struts Video Tutorials
on Struts framework. These video tutorials is easy to learn and understand. You...Struts Video Tutorials - Now you can learn the Struts programming easily and in less time. Struts Video tutorials are very extensive and explained
What is Struts?
What is a Struts? Understand the Struts framework This article tells you "What is a Struts?". Tells you how Struts learning is useful in creating... are now preferring Struts based applications. Struts is good as it provides
Struts 2.1.8 - Struts 2.1.8 Tutorial
Struts 2.1.8 - Struts 2.1.8 Tutorial       The Struts 2.1.8 is latest release of Struts 2 as on 28-April-2010.  In this tutorial we will understand the features of Struts 2.1.8
unable to open the service tomcat5
unable to open the service tomcat5  While trying to run tomcat server getting "unable to open the service tomcat5" error after installed tomcat
Struts
Struts   How to retrive data from database by using Struts
STRUTS
STRUTS  MAIN DIFFERENCES BETWEEN STRUTS 1 AND STRUTS 2
STRUTS
STRUTS  MAIN DIFFERENCES BETWEEN STRUTS 1 AND STRUTS 2
Struts
Struts  what is SwitchAction in struts
Struts
Struts  how to learn struts
STRUTS
STRUTS   Request context in struts? SendRedirect () and forward how to configure in struts-config.xml
struts
struts  in industry, struts 1 and struts 2. which is the best? which is useful as a professuional   Have a look at the following link: Struts Tutorials
Struts
Struts  Tell me good struts manual
struts
struts  what are the 4 methods of struts framework
struts
struts shopping cart project in struts with oracle database connection  shopping cart project in struts with oracle database connection   Have a look at the following link: Struts Shopping Cart using MySQL
Struts
Struts   When Submit a Form and while submit is working ,press the Refresh , what will happen in Struts
struts
struts  Hi what is struts flow of 1.2 version struts? i have struts applicatin then from jsp page how struts application flows Thanks Kalins Naik   Please visit the following link: Struts Tutorial
doubt in struts - Struts
doubt in struts   i don't understand the concept on Resource bundle in struts can u please help me out
struts - Struts
of struts, u understand wverything goto google type for strut-blank.jar...struts  hi, what is meant by struts-config.xml and wht are the tags... 2. wht is the difference b/w the web.xml and struts-config.xml 3. what
struts
struts   how to write Dao to select data from the database
struts
struts  why doc type is not manditory in struts configuration file
struts
struts  why doc type is not manditory in struts configuration file
struts
struts  why doc type is not manditory in struts configuration file
struts
struts  why doc type is not manditory in struts configuration file
Struts
Struts  Hi i am new to struts. I don't know how to create struts please in eclipse help me
struts
struts   Hi how struts flows from jsp page to databae and also using validation ? Thanks Kalins Naik
Struts
Setter methods of form bean class in Struts applications  who calls the setter methods of form bean class in struts applications
Struts
Struts   What is called properties file in struts? How you call the properties message to the View (Front End) JSP Pages
Struts
Struts   in struts I want two struts.xml files. Where u can specify that xml files location and which tag u specified
struts
struts  which is the best book to study struts on own? please tell me   You can learn struts through the following books: 1)Programming Jakarta Struts By Chuck Cavaness 2)Professional Jakarta Struts By James Goodwill
struts
struts  which is the best book to study struts on own? please tell me   You can learn struts through the following books: 1)Programming Jakarta Struts By Chuck Cavaness 2)Professional Jakarta Struts By James Goodwill
Struts
Struts  How Struts is useful in application development? Where to learn Struts? Thanks   Hi, Struts is very useful in writing web... performance Java web applications that runs on Java enabled application servers. Struts
Struts - Struts
Struts for beginners  struts for beginners example
struts
struts  how to start struts?   Hello Friend, Please visit the following links:ADS_TO_REPLACE_1 http://www.roseindia.net/struts/ http... through which you can easily learn the struts. ThanksADS_TO_REPLACE_2
struts
struts  i have no any idea about struts.please tell me briefly about struts?**   Hi Friend, You can learn struts from the given link:ADS_TO_REPLACE_1 Struts Tutorials Thanks
struts
struts  hi i would like to have a ready example of struts using "action class,DAO,and services" for understanding.so please guide for the same. thanks  Please visit the following link: Struts Tutorials
Struts
Struts  Am newly developed struts applipcation,I want to know how to logout the page using the strus   Please visit the following link: Struts Login Logout Application
Struts
Struts   1)in struts server side validations u are using programmatically validations and declarative validations? tell me which one is better ? 2) How to enable the validator plug-in file
struts
struts  please send me a program that clearly shows the use of struts with jsp
struts
struts  Hi, 1) can we write two controller classes in struts
struts
struts  Hi,... please help me out how to store image in database(mysql) using struts
struts
struts  how to generate bar code for continuous numbers in a loop using struts ... or atleast for a given number
Struts
Struts   How will you retrieve a collection of records from business logic to presentation logic in framework
Unable to Create DSN for
Unable to Create DSN for   I have installed Oracle 11g R2 and JDK7 in my Computer (Windows 7 Ultimate x64) . And, I want to work with Type 1 JDBC Driver. I'm unable to crate DNS for "Microsoft ODBC for Oracle", It gives eror
Unable to locate package openjdk-11-jdk
Unable to locate package openjdk-11-jdk  Hi, I want to install open jdk 11 on ubuntu with the following command: sudo apt install openjdk-11-jdk... information... Done E: Unable to locate package openjdk-11-jdk How to resolve
STRUTS
STRUTS  1)Have you used struts tag libraries in your application? 2)What are the various types of tag libraries in struts? Elaborate each of them? 3)How can you implement custom tag libraries in your application
struts
struts  Hi, Here my quation is can i have more than one validation-rules.xml files in a struts application
struts
struts  we have the concept of jsp's and servlets right we can develop the web-pages each and everything then why what for struts inturdouced

Ads