473,386 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Redirect from servlet to servlet

I want to insert data from Text box to oracle database ,so in my HTML where there are text box (and 3 Buttons-New ,Update and save)so when i click any of three buttons it takes me to redirectServlet , and from this servlet I want to again forward my page to insertServlet(i.e When I click NEW Button) ,but I am getting 404 error . May be my logic is wrong or wex.xml needs to be mapped properly . Can anyone help please Its urgent.
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  3.   <display-name>AcerLink</display-name>
  4.   <welcome-file-list>
  5.     <welcome-file>index.html</welcome-file>
  6.     <welcome-file>index.htm</welcome-file>
  7.     <welcome-file>index.jsp</welcome-file>
  8.     <welcome-file>default.html</welcome-file>
  9.     <welcome-file>default.htm</welcome-file>
  10.     <welcome-file>default.jsp</welcome-file>
  11.   </welcome-file-list>
  12.  
  13.      <servlet>
  14.  
  15.     <display-name>redirectServlet</display-name>
  16.     <servlet-name>redirectServlet</servlet-name>
  17.     <servlet-class>redirectServlet</servlet-class>
  18.   </servlet>
  19.   <servlet-mapping>
  20.     <servlet-name>redirectServlet</servlet-name>
  21.     <url-pattern>/redirectServlet</url-pattern>
  22.   </servlet-mapping> 
  23.  
  24.   <servlet>
  25.     <description></description>
  26.     <display-name>insertServlet</display-name>
  27.     <servlet-name>insertServlet</servlet-name>
  28.     <servlet-class>insertServlet</servlet-class>
  29.   </servlet>
  30.   <servlet-mapping>
  31.     <servlet-name>insertServlet</servlet-name>
  32.     <url-pattern>/insertServlet</url-pattern>
  33.   </servlet-mapping>
  34. </web-app>
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. import java.io.IOException;
  5. import java.io.PrintWriter;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.SQLException;
  9.  
  10. import javax.servlet.ServletException;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14.  
  15. /**
  16.  
  17.  * Servlet implementation class redirectServlet
  18.  
  19.  */
  20.  
  21. public class redirectServlet extends HttpServlet {
  22.  
  23.     private static final long serialVersionUID = 1L;
  24.  
  25.  
  26.     /**
  27.  
  28.      * @see HttpServlet#HttpServlet()
  29.  
  30.      */
  31.  
  32.     public redirectServlet() {
  33.  
  34.         super();
  35.  
  36.         // TODO Auto-generated constructor stub
  37.  
  38.     }
  39.  
  40.  
  41.     /**
  42.  
  43.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  44.  
  45.      */
  46.  
  47.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  48.  
  49.         // TODO Auto-generated method stub
  50.  
  51.  
  52.     }
  53.  
  54.     /**
  55.  
  56.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  57.  
  58.      */
  59.  
  60.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  61.  
  62.         // TODO Auto-generated method stub
  63.  
  64.         if (request.getParameter("NEW") != null) {
  65.  
  66.  
  67.             response.sendRedirect("/insertServlet");
  68.  
  69.  
  70.             return ;
  71.  
  72.  
  73.  
  74.         } else if (request.getParameter("UPDATE") != null) {
  75.  
  76.             request.getRequestDispatcher("/redirectServlet").forward(request, response);
  77.  
  78.  
  79.  
  80.  
  81.         }  else if (request.getParameter("SAVE") != null) {
  82.  
  83.             request.getRequestDispatcher("save.jsp").forward(request, response);
  84.  
  85.         }
  86.  
  87.     }
  88.  
  89. }
Expand|Select|Wrap|Line Numbers
  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.Statement;
  8.  
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13.  
  14. /**
  15.  * Servlet implementation class insertServlet
  16.  */
  17. public class insertServlet extends HttpServlet {
  18.     private static final long serialVersionUID = 1L;
  19.  
  20.     /**
  21.      * @see HttpServlet#HttpServlet()
  22.      */
  23.     public insertServlet() {
  24.         super();
  25.         // TODO Auto-generated constructor stub
  26.     }
  27.  
  28.     /**
  29.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  30.      */
  31.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  32.         // TODO Auto-generated method stub
  33.         PrintWriter out = response.getWriter();
  34.         PrintWriter pwinsert = response.getWriter();
  35. //      PrintWriter pwdelete = res.getWriter(); 
  36.  
  37.         Connection con = null;
  38.         PreparedStatement ps = null;
  39.        // ResultSet rs = null;
  40.        // Statement st = null;
  41.  
  42.         String appl=request.getParameter("app");
  43.         String sel=request.getParameter("second");
  44.         String url= request.getParameter("link");
  45.  
  46.         try {
  47.             Class.forName("oracle.jdbc.OracleDriver");
  48.         }
  49.         catch(ClassNotFoundException ex)    {
  50.             System.out.println("driver not loaded");
  51.             System.exit(0); 
  52.         } 
  53.         String URL = "jdbc:oracle:thin:SCOTT/tiger@computer_1:1521:orcl";
  54.         try {
  55.             con = DriverManager.getConnection(URL); 
  56.  
  57.             if(request.getParameter("choise")==null)    {
  58.                 ps = con.prepareStatement("INSERT into TEST_LINKS (ID,APPLICATION,ENTITY,URL) VALUES(TEST_LI.nextval,?,?,?)"); 
  59.                 ps.setString(1,appl);
  60.                 ps.setString(2,sel); 
  61.                 ps.setString(3,url); 
  62.  
  63.                 int i = ps.executeUpdate(); 
  64.                 pwinsert.println(i); 
  65.  
  66.                 if(i!=0)    { 
  67.                     pwinsert.println("Your data has been stored in the database"); 
  68.                 } 
  69.                 else    { 
  70.                     pwinsert.println("Your data could not be stored in the database"); 
  71.                 } 
  72.             }
  73.         } 
  74.         catch(Exception e)  {
  75.             pwinsert.println(e.getMessage());
  76.         } 
  77.  
  78.     }
  79.  
  80.     /**
  81.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  82.      */
  83.     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  84.         // TODO Auto-generated method stub
  85.     }
  86.  
  87. }
  88.  
Nov 2 '13 #1

✓ answered by r035198x

1.) Use standard Java conventions for naming class files. Class names should start with an upper case letter.
2.) Put your servlets in a user defined package.
3.) What link did you specify in the JSP for the submit? It needs to match the value of the mapping you have in your web.xml.

1 2661
r035198x
13,262 8TB
1.) Use standard Java conventions for naming class files. Class names should start with an upper case letter.
2.) Put your servlets in a user defined package.
3.) What link did you specify in the JSP for the submit? It needs to match the value of the mapping you have in your web.xml.
Nov 4 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Hai Tran | last post by:
Any help is appreciated. Installed Tomcat 4.1 and Mysql on a WinXP. I've manage to get Tomcat up and was able to view my first application ( myhome ) simple home page. Tomcat was installed under...
14
by: ramadeviirrigireddy | last post by:
Hi All, I have the following code for form and servlet. when the form is submitted the servlet will print the values passed by the form. i'm not getting the servlet o/p when i submit the...
4
by: shaileshkumar | last post by:
hi, when i type javap javax.servlet.Servlet on the command propmpt iam gettting the following error Error:could not find javax.servlet.Servlet plz help me proper instructions to make...
0
by: krishna81m | last post by:
Could some one please explain why the session is not being maintained when I am doing a forward in a servlet after setting a cookie. I am even unable to set session attributes or parameters and...
6
by: Sushmita | last post by:
hi all, I have wrriten code for a small web application. From my JSP page i am not able to call the servlet.. its throwing an exception. "javax.servlet.ServletException: Wrapper cannot find...
1
by: shiyamala | last post by:
Hi i am shiyam, i am having some problem in java, i am basic java programmer. i have to write one program is writing the data into textfile name "newfile.txt" from one servlet and read it from...
9
by: mjahabarsadiq | last post by:
Hi I have created a servlet that is to be started at the server startup. And I got it. In that I have created a object of another class and set it as a session attribute. What I am trying is...
2
by: jerald m | last post by:
Hi, For Login authentication. i create JSP Form. login.jsp(webapps/loginsystem/login.jsp) <form method="POST" action ="http://localhost:8085/loginsystem/lo" name="login"> <font...
1
by: madshov | last post by:
Hello everyone, I have spent a couple of days now trying to figure out why I can't serve my servlets through tomcat. I have no experience with tomcat, so I'm running out of ideas. Basically I...
0
by: enzo | last post by:
I want to create a maven scala spring hibernate project **using ECLIPSE**. Im following this tutorial : http://grahamhackingscala.blogspot.fr/2010/01/scala-spring-hibernate-maven-webapp-how.html ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.