This week's book giveaway is in the
Agile and other Processes
forum.
We're giving away four copies of
The Mikado Method
and have Ola Ellnestam and Daniel Brolund on-line!
See
this thread
for details.
A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Java
»
Servlets
Author
not redirecting to main page
Megha Singhal
Ranch Hand
Joined: Feb 28, 2012
Posts: 128
I like...
posted
Mar 29, 2012 23:23:52
0
i tried almost 100 times but i don't know why the page is not redirecting to main page after performing deletion in the following code
import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; import java.lang.String; import java.sql.Connection; import javax.servlet.RequestDispatcher; import java.sql.Statement; import java.sql.ResultSet; import javax.naming.*; import javax.naming.directory.*; import oracle.jdbc.pool.OracleDataSource; public class DeleteUser extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); Connection con = null; Statement st1 = null; ResultSet rs = null; String str = request.getQueryString(); int P_ID = Integer.parseInt(str); try { Context initContext = new InitialContext(); Context envContext = (Context) initContext.lookup("java:/comp/env"); OracleDataSource ds = (OracleDataSource) envContext.lookup("jdbc/abc"); if (envContext == null)throw new Exception("Error: No Context"); if (ds == null) throw new Exception("Error: No DataSource"); if (ds != null) con = ds.getConnection(); st1 = con.createStatement(); ArrayList al=null; ArrayList userList =new ArrayList(); String query = "delete from LTCINFO.PERSONS where P_ID="+P_ID; int i = st1.executeUpdate(query); if(i>=0) { response.sendRedirect("ServletRecord"); } rs.close(); st1.close(); con.close(); } catch (SQLException se) { out.println("An Error Had occurred while accessing the database"); out.println("<HR>"); out.println(se.getMessage()); } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (st1 != null) st1.close(); if (con != null) con.close(); } catch (Exception e) { out.println(e); } } } }
but it is redirecting in my following code of addition
import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; import java.lang.String; import java.sql.Connection; import javax.servlet.RequestDispatcher; import java.sql.Statement; import java.sql.ResultSet; import javax.naming.*; import javax.naming.directory.*; import oracle.jdbc.pool.OracleDataSource; public class AddUser extends HttpServlet{ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ response.setContentType("text/html"); PrintWriter out = response.getWriter(); String P_ID = request.getParameter("P_ID"); int id = Integer.parseInt(P_ID); String LASTNAME = request.getParameter("LASTNAME"); String FIRSTNAME = request.getParameter("FIRSTNAME"); String ADDRESS = request.getParameter("ADDRESS"); String CITY = request.getParameter("CITY"); Connection con = null; Statement st1 = null; ResultSet rs = null; try { Context initContext = new InitialContext(); Context envContext = (Context) initContext.lookup("java:/comp/env"); OracleDataSource ds = (OracleDataSource) envContext.lookup("jdbc/abc"); if (envContext == null)throw new Exception("Error: No Context"); if (ds == null) throw new Exception("Error: No DataSource"); if (ds != null) con = ds.getConnection(); st1 = con.createStatement(); ArrayList al=null; ArrayList userList =new ArrayList(); String query = "insert into LTCINFO.PERSONS(P_ID,LASTNAME,FIRSTNAME,ADDRESS,CITY) values ("+id+",'"+LASTNAME+"','"+FIRSTNAME+"','"+ADDRESS+"','"+CITY+"')"; int i = st1.executeUpdate(query); if(i>0) { response.sendRedirect("ServletRecord"); } rs.close(); st1.close(); con.close(); } catch (SQLException se) { out.println("An Error Had occurred while accessing the database"); out.println("<HR>"); out.println(se.getMessage()); } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (st1 != null) st1.close(); if (con != null) con.close(); } catch (Exception e) { out.println(e); } } } }
i am not getting if everything is same then why it is not redirecting to the main page.
Maneesh Godbole
Saloon Keeper
Joined: Jul 26, 2007
Posts: 8434
I like...
posted
Mar 29, 2012 23:49:51
0
Please do not post, essentially the same question, multiple times.
Let us continue the discussion in your original topic here
http://www.coderanch.com/t/571694/Servlets/java/exception-while-running-code
[
Donate a pint, save a life!
] [
How to ask questions
] [
Onff-turn it on!
]
I agree. Here's the link:
http://ej-technologies/jprofiler
- if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
subject: not redirecting to main page
Similar Threads
exception while running code
page gets hang on runtime
Exception coming while connecting to database
compilation error
error of missing values
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter