Hi All,
I have got one problem with JSP.My problem is how to use GET and POST
method simultaneously in a JSP program.I have 2 buttons,one is ADD to
insert record in to the database,and other is FIND to locate the record in
the database by giving keyfield.I m using JSP and BEANS.For ADD i have to
use POST,and for FIND I have to use GET.How can i solve this problem.I
tried to solve it in different ways.One is to use Javascript,but that did
not work.I do not know how to solve it using only JSP.I m sending my codes
for the discussion amongest ourselves.I will be grateful to u,If u can
send me after going through my codes carefully.
Looking forwards to getting reply from u at the earliest.
Thanx

The codes without javascript is(solely on JSP)

AddMain.jsp

<html>
<head>
<title>
Data Entry Screen
</title>
</head>
<body>

<%@ page language="java" %>

Data Entery:

<form method="POST" action="Add2.jsp">

Enter Name:

<p>

<input type=text name=inputSQL1 size=10>

<p>

Enter Phone:

<p>

<input type=text name=inputSQL2 size=10>

<p>


<input type="submit" name="action" value="add">
<input type="submit" name="action" value="del">
<input type="submit" name="action" value="mod">
</form>
</body>
</html>

Add2.jsp is

<html>
<head>
<title>
Database Search
</title>
</head>
<body>

<%@ page language="java" import="java.sql.*" %>


<jsp:useBean id="db" scope="request" class="com.wrox.jspexamples.Bean1" />

<jsp:setProperty name="db" property="*" />

<%! int numColumns;

%>

<center>
<%
   String sql1 = request.getParameter("inputSQL1");
   String sql2 = request.getParameter("inputSQL2");
   String bT = request.getParameter("action");


   try {
      db.connect();
   } catch (ClassNotFoundException e1) {
      throw new ServletException("Database drivers not available", e1);
   } catch (SQLException e2) {
      throw new ServletException("Database URL is wrong", e2);
   }

 if(bT.trim().equals("add"))
{

   try {
       db.insert(sql1,sql2);

   } catch (SQLException e3) {
      throw new ServletException("Your query isn't working. " +
                                 "Do you want to browse the database? " +
                                 "If so, leave the SQL input empty", e3);
   }

}

if(bT.trim().equals("del"))
{

   try {
       db.delete(sql1);

   } catch (SQLException e4) {
      throw new ServletException("Your query isn't working. " +
                                 "Do you want to browse the database? " +
                                 "If so, leave the SQL input empty", e4);
   }

}

if(bT.trim().equals("mod"))
{

   try {
       db.update(sql1,sql2);

   } catch (SQLException e5) {
      throw new ServletException("Your query isn't working. " +
                                 "Do you want to browse the database? " +
                                 "If so, leave the SQL input empty", e5);
   }

}

try {

       db.close();
   } catch (SQLException e5) {
      throw new ServletException("Database error. The query worked, " +
                                 "but the display didn't", e5);
   }


%>

</center>
</body>
</html>

Bean1.java is
package com.wrox.jspexamples;

import java.sql.*;
import java.io.*;

public class Bean1 {

   String dbURL;

   Statement s;

   private Connection dbCon;

   public Bean1() {
      super();
   }

   public boolean connect() throws ClassNotFoundException, SQLException {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      dbCon =
DriverManager.getConnection("jdbc:odbc:prjct1","scott","tiger");
      return true;
   }

   public void close() throws SQLException {
      dbCon.close();
   }



   public void insert(String sq1,String sq2) throws SQLException {

      s = dbCon.createStatement();
      s.executeUpdate("INSERT INTO EMP1 VALUES('" + sq1 +
                     "','" + sq2 + "')");
   }

  public void delete(String sq3) throws SQLException {

      s = dbCon.createStatement();
      s.executeUpdate("DELETE FROM  EMP1 WHERE NAME = '" +sq3+ "'");
   }

  public void update(String sq4,String sq5) throws SQLException {

      s = dbCon.createStatement();
      s.executeUpdate("UPDATE EMP1 SET PHONE = '" + sq5 +"' WHERE NAME =
'" +sq4+ "'");
   }


   public int getDbDriver() {
      return 0;
   }

   public void setDbDriver(String newValue) {

   }


}
 with javascript is(Addmain.jsp)
<html>
<head>
<title>
Data Entry Screen
</title>

<script language="javascript">

function fun1()
{
 document.frm[0].method="POST"
  return false
}

function fun2()
{
 document.frm.method="GET"
 return false
}

</script>

</head>
<body>
Data Entery:
<form name="frm" action="http://147.0.1.41:8080/examples/jsp/Add2.jsp">

Enter Name:

<p>
<input type=text name=inputSQL1 size=10>

<p>
Enter Phone:

<p>

<input type=text name=inputSQL2 size=10>

<p>

<input type="submit" name="action" value="add" onclick="fun1()">
<input type="submit" name="action" value="del" onclick="fun1()">
<input type="submit" name="action" value="mod" onclick="fun1()">
<input type="submit" name="action" value="find" onclick="fun2()">
</form>
</body>
</html>

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to