Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Struts Unable to understand Struts

Have Programming Question? Ask it here!
 
 
 


Abhishek
Unable to understand Struts
0 Answer(s)      4 years and 3 months ago
Posted in : 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"/>

Advertisement
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
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 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
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
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
unable to connect database in java
unable to connect database in java  Hello Everyone! i was trying to connect database with my application by using java but i am unable to connect... i was using this code.... try { Driver d=(Driver)Class.forName
Unable to execute JSP page
Unable to execute JSP page  I have written one jsp file. It contains html tags and jsp directives. I have saved the file with the extension .jsp. The tomcat server is already running onto my machine. I have saved the jsp file
Unable to use JMXMP with Knopflerfish?
Unable to use JMXMP with Knopflerfish?  I have a bundle which runs properly with Eclipse, then I generate the jar file and try to use it with Knopflerfish, then I get the following exception java.net.MalformedURLException
Please help me understand this query
Please help me understand this query  Hi, Could someone please help me understand the following query. Im finding difficulty relating it :-( Pls Help! select * from cbpartner where CBPartner.IsSummary='N' and salesrep_id
Unable to compile Servlet
Unable to compile Servlet  First I installed the tomcat server in C:\apache-tomcat-6.0.14 directory. Then I created a system variable CATALINA_HOME with the value C:\apache-tomcat-6.0.14. Then I created another system variable
unable to connect database in java
unable to connect database in java  Hello Everyone! i was trying to connect database with my application by using java but i am unable to connect... i was using this code. try { Driver d=(Driver)Class.forName
Unable to store the image into database
Unable to store the image into database  Hello, I have created below program and program is throwing FileNotFound exception when I pressed the submit button. The problem is coming due to JSP page is not able to find complete path
unable to insert data into database
unable to insert data into database    hello.i have a problem in inserting data into database.i have used two prepared statement.one for retrieving the eid based on ename and the other is inserting data into database based
unable to insert data into database
unable to insert data into database    hello.i have a problem in inserting data into database.i have used two prepared statement.one for retrieving the eid based on ename and the other is inserting data into database based
unable to insert data into database
unable to insert data into database    hello.i have a problem in inserting data into database.i have used two prepared statement.one for retrieving the eid based on ename and the other is inserting data into database based
unable to insert data into database
unable to insert data into database    hello.i have a problem in inserting data into database.i have used two prepared statement.one for retrieving the eid based on ename and the other is inserting data into database based
unable to run servlet
unable to run servlet  Dear sir/madam i have jdk1.5.0_04 and tomcat server 1.0.16 installed on my computer. on my computer servlet is compiling properly on command prompt but during servlet execution on browser with following url
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 import class com.opensymphony.xwork2.ActionContext not found
unable to import class com.opensymphony.xwork2.ActionContext not found  Imported class com.opensymphony.xwork2.ActionContext not found gettin the above error
Unable to compile class for JSP - WebSevices
Unable to compile class for JSP  org.apache.jasper.JasperException: Unable to compile class for JSP When I am trying to access a java file kept under src folder under a package from a jsp page placed under web directory, I am
unable to connect to server - JSP-Servlet
unable to connect to server  thank you for the program code for inserting and reteriving the image but i am unable to deploy these two programs in weblogic/apache servers can any one help me how to do this ?   Hi Friend
What do you understand by private, protected and public?
What do you understand by private, protected and public?   Hi, What do you understand by private, protected and public? thanks
Unable to install Subversion plugins in MyEclipse
Unable to install Subversion plugins in MyEclipse  Thanks for your reply Deepak. I am using MyEclipse Blue Edition 8.6 M1. I tried adding Subversion plugins and got error. This is the path i followed: Help --> MyEclipse
Unable to connect servet, jsp to oracle 10g database.. Unable to retrieve data..
Unable to connect servet, jsp to oracle 10g database.. Unable to retrieve data..  I have a class file AbstractDataAccessObject with the below code.... If I use it also, unable to connect to backend oracle database. Let me
Unable to create annotations in hibernate 4.0
Unable to create annotations in hibernate 4.0  I worked on a similar example as yours.But when I create a new hibernate console configuration for this new project,I get this error,the console configuration gets created I but when
unable to see the output of applet. - Applet
unable to see the output of applet.  Sir, I was going through the following tutorial http://www.roseindia.net/java/example/java/applet/FirstApplet.shtml of your website. this page has got the "link" which says Try online
"unable to load tag handler struts.views.jsp.ui.Form tag "
"unable to load tag handler struts.views.jsp.ui.Form tag "   while working with tags i show"unable to load tag handler struts.views.jsp.ui.Form tag " same error is also with iterator tag
ORA-01019: unable to allocate memory in the user side
ORA-01019: unable to allocate memory in the user side  when i trying to oracle i got the following error.. ORA-01019: unable to allocate memory in the user side... How to resolve in oracle
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
Struts2...problem in JSP..unable to get the values for menuTitle!!!
Struts2...problem in JSP..unable to get the values for menuTitle!!!  ..." encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd
i am unable to identify the error in my code
i am unable to identify the error in my code  class Program { public static void main(String[] args) { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter
i am unable to identify the error in my code
i am unable to identify the error in my code  class Program { public static void main(String[] args) { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter
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  what are the 4 methods of struts framework
Struts
Struts  Tell me good struts manual
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
Unable to find Software Updates in Help menu in MyEclipse
Unable to find Software Updates in Help menu in MyEclipse  Hi, I am using MyEclipse for my project. I have to use SVN repository for checking the code and need to install Subclipse plugins to MyEclipse. But I am not getting
Unable to send mail using php script
Unable to send mail using php script  Hello i am trying to send mail using mail function.But the message is never sent. I have installed a free smtp server which i run in background before executing the code.and my php.ini file
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: Struts Tutorials Thanks
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
 

 

 

DMCA.com