Visualizzazione dei risultati da 1 a 7 su 7
  1. #1

    [JSP] interfacciare sito jsp con database mysql

    Ciao a tutti,
    sto creando un sito in jsp che si deve interfacciare con un data base MySQL. Ho cominciato a scrivere una pagina login.html che rimanda con il form alla pagina login.jsp [qui sotto]

    codice:
    <%@ page language="java" import="java.sql.*, javax.sql.*, javax.naming.*" %>
    
    <%@ include file="connection.jsp" %>
    
    <%! boolean checkUserAccount(Statement st, String username, String password) throws SQLException {
    	ResultSet rs = st.executeQuery("SELECT * FROM utente WHERE username='"+username+"' AND password='"+password+"'");
    	if (rs.next()) return true;
    	return false;
    } %>
    
    <%
    String username=request.getParameter("username");
    String password=request.getParameter("password");
    
    if (checkUserAccount(statement, username, password)) {
    	session.setAttribute("logged","true");
    	session.setAttribute("username","admin");
    	response.sendRedirect("index_user.html");
    }
    else response.sendRedirect("index.html");
    
    connection.close();
    
    %>
    questa include il file connection.jsp

    codice:
    <%@ page language="java" import="java.sql.*, javax.sql.*, javax.naming.*"%>
    
    <%
    Connection connection;
    ResultSet rs;
    Context initContext = new InitialContext();
    Context envContext  = (Context)initContext.lookup("java:/comp/env");
    DataSource ds = (DataSource)envContext.lookup("jdbc/ConnTm08_09");
    connection = ds.getConnection();
    Statement statement=connection.createStatement();
    %>
    inoltre ho creato anche il file tm08_09.xml che ho messo in C:\Programmi\Apache Software Foundation\Tomcat 5.5\conf\Catalina\localhost così fatto:

    codice:
    <Context docBase="C:/Programmi/Apache Software Foundation/Tomcat 5.5/webapps/tm08_09" crossContext="true" reloadable="true" path="/tm08_09" debug="0">
    
    <Logger suffix=".log" prefix="tm09." className="org.apache.catalina.logger.FileLogger" timestamp="true"/>
    <Resource name="jdbc/ConnTm09" scope="Sharable" auth="Container" type="javax.sql.DataSource" factory="org.apache.commons.dbcp.BasicDataSourceFactory" 
    url="jdbc:mysql://localhost:3306/tm08_09?autoReconnect=true" driverClassName="com.mysql.jdbc.Driver" username="tm08_09" password="qwerty" maxIdle="30" maxActive="80" maxWait="10000" removeAbandoned="true" 
    removeAbandonedTimeout="60" logAbandoned="true"/>
    </Context>
    Ho anche messo il file mysql-connector-java-5.0.8-bin.jar in C:\Programmi\Apache Software Foundation\Tomcat 5.5\common\lib
    Quando cerco di fare il login però tomcat mi da errore scrivendo..

    codice:
    exception
    
    org.apache.jasper.JasperException: Exception in JSP: /connection.jsp:8
    
    5: ResultSet rs;
    6: Context initContext = new InitialContext();
    7: Context envContext  = (Context)initContext.lookup("java:/comp/env");
    8: DataSource ds = (DataSource)envContext.lookup("jdbc/ConnTm08_09");
    9: connection = ds.getConnection();
    10: Statement statement=connection.createStatement();
    11: %>
    riferendosi alla riga del data base [il cui nome è tm08_09].. solo che non capisco che cosa ho sbagliato nella configurazione.. non riesco a far accedere le pagine jsp al data base..

    grazie a chiunque mi dia un aiuto..

  2. #2
    <Resource name="jdbc/ConnTm09

    ...

    DataSource ds = (DataSource)envContext.lookup("jdbc/ConnTm08_09 ");


    Hai messo nomi differenti... dovrebbe essere questo il problema
    Al mio segnale... scatenate l'inferno!

  3. #3
    hai ragione anche se alla fine [nn so se per quello o per qualcos'altro] l'errore ora è questo
    codice:
    exception
    
    org.apache.jasper.JasperException: Name jdbc is not bound in this Context
    	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    
    root cause
    
    javax.servlet.ServletException: Name jdbc is not bound in this Context
    	org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:841)
    	org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:774)
    	org.apache.jsp.login_jsp._jspService(login_jsp.java:94)
    	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    
    root cause
    
    javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
    	org.apache.naming.NamingContext.lookup(NamingContext.java:770)
    	org.apache.naming.NamingContext.lookup(NamingContext.java:153)
    	org.apache.jsp.login_jsp._jspService(login_jsp.java:67)
    	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    però ancora nn mi è chiaro come funzioni (DataSource)envContext.lookup("jdbc/ConnTm08_09").. che cosa sarebbe jdbc/Conn ??

  4. #4
    Segui l'esempio riportato qui. Ti sarà sicuramente più utile di quanto possa fare io
    Al mio segnale... scatenate l'inferno!

  5. #5
    humm.. sinceramente cm esempio nn è molto chiaro..

    a parte il fatto che appena cerco di configurare il file server.xml non mi parte più tomcat [io aggiungo le cose sopra al tag </Host> come dice lui ma nulla].. e poi nn riesco a capire bene la sintassi che devo usare per il comando (DataSource)envContext.lookup(" ")

    come faccio a sapere dato il mio database quali file devo configurare? io sapevo che bastava creare un file con il nome del database e estensione xml e metterlo in C:\Programmi\Apache Software Foundation\Tomcat 5.5\conf\Catalina\localhost il cui testo è appunto quello che ho postato sopra [il terzo blocco di codice del primo post]..

  6. #6
    nessuno riesce a spiegarmi l'uso del codice che ho postato sopra??

    codice:
    <%@ page language="java" import="java.sql.*, javax.sql.*, javax.naming.*"%>
    
    <%
    Connection connection;
    ResultSet rs;
    Context initContext = new InitialContext();
    Context envContext  = (Context)initContext.lookup("java:/comp/env");
    DataSource ds = (DataSource)envContext.lookup("jdbc/ConnTm08_09");
    connection = ds.getConnection();
    Statement statement=connection.createStatement();
    %>
    nn riesco a copire come inserire il nome del database [che sarebbe tm08_09] perchè li dove l'ho messo nn funziona..
    penso però che si debba connettere in qualche modo con il file xml che contiene la password..

  7. #7
    Nella cartella della tua webapp c'è la cartella META-INF. All'interno di questa cartella c'è un file context.xml

    all'interno di questo file scrivi:
    codice:
    <?xml version="1.0" encoding="UTF-8"?>
    <Context path="/NomeDellaTuaWebapp" docBase="NomeDellaTuaWebapp " debug="5" reloadable="true" crossContext="true">
         
         
         
         
         
         
         <Resource name="jdbc/ConnTm09" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="UserPerDB " password="PasswordPerDB " driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/tm08_09 ?autoReconnect=true"/>
    </Context>
    Quindi il file web.xml della tua webapp sarà una cosa del genere:

    codice:
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
        version="2.4">
      <description>MySQL Test App</description>
      <resource-ref>
          <description>DB Connection</description>
          <res-ref-name>jdbc/ConnTm09</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <res-auth>Container</res-auth>
      </resource-ref>
    </web-app>
    per accedervi da una classe o da una jsp:

    DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/ConnTm08_09");
    if (ds != null) {
    Connection conn = ds.getConnection();
    ...
    Al mio segnale... scatenate l'inferno!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.