Acces to a servlet...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • elchape
    New Member
    • Nov 2008
    • 1

    Acces to a servlet...

    Hi,

    I truly make a lot of atemps, and I decided to ask to you
    I have this web

    I have to make a java application to make this form request and save the result page that if you see the source code apears that the form action connect to
    Code:
    /servlet/CRManFLlega
    I ask you if someone knows how to make this connection with a java console application, only to save the result
    Thanks a lot and I hope toy can help me
  • Dököll
    Recognized Expert Top Contributor
    • Nov 2006
    • 2379

    #2
    Greetings!

    I saw servlet, and I jumped. I did not go to the site. Can you tell us a little more? Can you add the full code, you might get better luck that way...

    Have a look here meanwhile, I could not say too much was understandable but I did get the fact that you need to retrieve something:

    [CODE=JAVA]

    //import packages/libraries

    import java.io.*;
    import java.sql.*;
    import javax.servlet.* ;
    import javax.servlet.h ttp.*;

    //start you request
    public class NameOfYourServl et extends HttpServlet{
    public void doGet(HttpServl etRequest request,
    HttpServletResp onse response) throws
    ServletExceptio n, IOException{

    //tell the servlet the type of gui the results will load in, in this case, HTML...

    response.setCon tentType("text/html");
    PrintWriter pw = response.getWri ter();

    //You should have had an ODBC connection ready,
    //with a DataSource name Northwind.
    //Your database name can be anyhting,
    //The datasource is most important here.

    String connectionURL = "jdbc:odbc:Nort hwind";
    Connection connection=null ;
    try{

    //Connect to the odbc driver specific to Sun Microsystems' platform(s)...

    Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
    connection = DriverManager.g etConnection(co nnectionURL, "root", "root");

    //it is advisable to use a preparedStateme nt here, but this should do...
    //Begin running the query to retrieve userid and password info.

    Statement st = connection.crea teStatement();
    ResultSet rs = st.executeQuery ("Select * from YourTable");
    while(rs.next() ){

    //add the result to an HTML page, no graphics, but you'll get the point...

    pw.println("You rName" + " " + "YourPasswo rd" + "<br>");
    pw.println(rs.g etString(1) + " " + rs.getString(2) + "<br>");
    }
    }

    //barring any unforseen adventures withe code, you should be good to go...
    //but in the case there are problems, it will be caught here.
    //Also see above where the exception is defined.

    catch (Exception e){
    pw.println(e);
    }
    }
    }
    [/CODE]

    We hope you have a bit of xml experience, you'll need it going forward. Below however is prety simple but needed to make available the servlet, in conjunction to the HTML gui:

    [CODE=XML]

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!--<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd"> -->

    <web-app>
    <servlet>
    <servlet-name>FancyServl et</servlet-name>
    <servlet-class>YourServl etName</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>FancyServl et</servlet-name>
    <url-pattern>/YourServletName </url-pattern>
    </servlet-mapping>
    </web-app>

    [/CODE]

    web.xml file is crucial to firing you HTML, without it your Servlet will not run...

    Hope this is what you're looking for. I would also search here just to see if anything's posted.

    In a bit!
    Last edited by Dököll; Nov 20 '08, 03:57 AM. Reason: hoever : o (-)

    Comment

    Working...