Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2008

Tip: Looking for answers? Try searching our database.

Problem with a  servlet

Thread view: 
frusciante - 22 Feb 2008 17:42 GMT
Salve a tutti.
Il mio problema è: Devo passare dei parametri da una form in html ad un
servlet.La servlet prende i parametri li confronta e mi da come uotput un
codice fiscale.Ora credo che il mio problema sia nel codice della servlet
perchè eclipse mi segnala errore nella dichiarazione dei metodi httprequest
e response. Qualcuno riesce a risolvermi il problema??
Ora vi posto il codice:
     Codice:
     ecco l html questa è la pagina   form.html

hello boy and girl.
I am italian and i not speak english very well.
I have a problem with a serlvet.
I hava a page html that send the  parameters "nome" and "cognome" to a
servlet.
The servlet receve the parameter and wrtie on video the "cf"
My eclipse sign the error on declaration of httprequest and http response .

This is a page .html
     <!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=ISO-8859-1">
     <title>Inserisci </title>
     </head>
     <body>
     <form method ="post" action ="Cod_fisc.java">
     Nome Utente <input type="text" name="nome"> <br>
     Cognome Utente <input type="text" name="cognome"><br>
     <input type ="submit" value= "invia">
     </form>
     </body>
     </html>

     ********************
     this is Cod_fisc.java

     import java.io.IOException;
     import java.sql.Connection;
     import java.sql.DriverManager;
     import java.sql.ResultSet;
     import java.sql.SQLException;
     import javax.servlet.*;                //  sign error
     import javax.servlet.http.*;          // error

     public class Cod_fisc {
        public void doGet(HttpServletRequest req, HttpServletResponse res)
// eclipse write error
         throws ServletException, IOException                     //
eclipse write error
        {

           Connection con = null;
           String cf;
           String nome_reg=req.getParameter("nome");
           String cognome_reg=req.getParameter("cognome");

        try{
        Class.forName("com.mysql.jdbc.Driver");
        con=DriverManager.getConnection("jdbc:mysql://localhost:3306/db","usr","pwd");
        System.out.println("OK");
        }
        catch(Exception e ){
           System.out.println("fallita");
           e.printStackTrace();
           System.exit(1);
        }

        try{
        java.sql.PreparedStatement st =con.prepareStatement("Select cf from
utente where nome=? and cognome =?");
        st.setString(1,cf);

        ResultSet rs= st.executeQuery();
        while (rs.next()){
           System.out.println("cf: " + rs.getString(1));

        }
        rs.close(); // result set
        st.close(); // statement
        con.close();
          }
      catch (SQLException  e) {
         System.out.println("errore ");
         }
      }
     }
wizard of oz - 22 Feb 2008 22:38 GMT
Problem 1 your servlet should extend HttpServlet. I do not use eclipse but
it should have a "new servlet" function and give you the correct code
template.
Problem 2 you must register your servlet in the web.xml. Eclipse should do
this automatically when you create the servlet using the "new servlet"
function. An example of this is:
   <servlet>
       <servlet-name>MyServlet</servlet-name>
       <servlet-class>Cod_fisc</servlet-class>
   </servlet>
   <servlet-mapping>
       <servlet-name>MyServlet</servlet-name>
       <url-pattern>/servlet/MyServlet</url-pattern>
   </servlet-mapping>

Problem 3 you don't nominate the java file in your web page. You use the
url-pattern. Example:
 <form method ="post" action
="http://localhost:8080/myWebApp/servlet/MyServlet">
or
 <form method ="post" action ="servlet/MyServlet">

Suggestion 4 you should put your servlet in a package. Example
>      this is Cod_fisc.java

package com.myorg.servlet;
import java.io.IOException;
import java.sql.Connection;
....

If you do this, then the only change to web.xml is:
       <servlet-class>com.myorg.servlet.Cod_fisc</servlet-class>

Problem 5, your servlet doesn't do anything, it simply exits. What should
happen next? Do you want the servlet to generate a web page? If so, use
out.println statements. Example:
   out.println ("<html>");
   out.println ("<body>");
   etc.
Or redirect to another page:
           response.sendRedirect(request.getContextPath() + "/index.jsp");
Note: You can generate output or send a redirect. You can not do both. If
you try to do both (e.g. out.println and then decide to redirect) you will
get an error.

Suggestion 6. You could make your code shorter by combining the two try
catch blocks. There is no real need to seperate them. Example:
>         try{
>         Class.forName("com.mysql.jdbc.Driver");
[quoted text clipped - 16 lines]
>       catch (SQLException  e) {
>          System.out.println("errore ");
           e.printStackTrace ();
>          }
       catch (ClassNotFoundExecption e) {
            e.printStackTrace ();
      }
You could combine the 2 catches into one, but I like to leave them seperate.
It reminds me that there are two possible types of problem if I decide to do
something with them later.

Good luck

> Salve a tutti.
> Il mio problema è: Devo passare dei parametri da una form in html ad un
[quoted text clipped - 89 lines]
>       }
>      }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.