2015-09-24 38 views
1

我正在JSP和Java(servlet)中創建一個簡單的Maven Web應用程序。我的webapp的功能是通過數據庫搜索註冊的人。它搜索ID或名稱。所以我有一個帶有表單的JSP,將數據發送到我的servlet,而在servlet中,我調用了一個Java類中的函數,它可以建立數據庫連接並獲取數據。從servlet調用Java函數,函數將不會執行

但我提到該函數從來沒有調用或別的東西,我不知道如何。任何人都可以幫忙嗎?

web.xml

<!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> 
    <display-name>Archetype Created Web Application</display-name> 
    <servlet> 
    <servlet-name>TestServlet</servlet-name> 
    <display-name>TestServlet</display-name> 
    <description></description> 
    <servlet-class>mvnproject.servlet.TestServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>TestServlet</servlet-name> 
    <url-pattern>/TestServlet</url-pattern> 
    </servlet-mapping> 
</web-app> 

JSP:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"> 
    <jsp:directive.page contentType="text/html; charset=ISO-8859-1" 
     pageEncoding="ISO-8859-1" session="false"/> 
    <jsp:output doctype-root-element="html" 
     doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" 
     doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 
     omit-xml-declaration="true" /> 
    <jsp:directive.page import="mvnproject.*" /> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title>Serch</title> 
</head> 
<body> 
<h1>Zoek Persoon</h1> 
     <form action="TestServlet" method="POST"> 
      <select name="serchOn"> 
      <option value="ID" name="ID">ID</option> 
      <option value="name" name="name">name</option> 
      </select> 
      <input type="text" name="input"></input> 
      <input type="submit" placeholder="Serch"></input> 
     </form> 
</body> 
</html> 
</jsp:root> 

的Servlet:

package mvnproject.servlet; 

import java.io.IOException; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.apache.catalina.connector.Request; 

/** 
* Servlet implementation class TestServlet 
*/ 
public class TestServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#HttpServlet() 
    */ 
    public TestServlet() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 
     response.getWriter().append("Served at: ").append(request.getContextPath()); 

     Functions f = new Functions(); 

     String name = request.getParameter("serchOn"); 
     String input = request.getParameter("input"); 
     System.out.println(" " + name + " " + input); 
     System.out.println("calling db conn function"); 
     f.dbConn(name, input); 
     System.out.println("Script completed"); 

    } 



    /** 
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 



    } 

} 

Java函數:

import java.sql.*; 

public class Functions { 
    public void dbConn(String nVal, String inpVal){ 

    System.out.println("Running function..."); 

    if(nVal != null || inpVal != null){ 
     String sqlSerch; 
     if(nVal.equals("name")){ 
      sqlSerch = "ID, aNaam FROM profiles WHERE naam = " + nVal; 
     }else{ 
      sqlSerch = "naam, aNaam FROM profiles WHERE ID = " + nVal; 
     } 

     //driver/db path 
      final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; 
      final String DB_URL = "jdbc:mysql://localhost/profile"; 
      //DB user&password 
      final String USER = "root"; 
      final String PASS = "Ciber2015!"; 
      //declare con & sql var 
      Connection conn = null; 
      Statement stmt = null; 
      //register jdbc driver 
      try{ 
       Class.forName(JDBC_DRIVER); 
       //make a connection 
       conn = DriverManager.getConnection(DB_URL,USER,PASS); 
       //SQL Statement 
       stmt = conn.createStatement(); 
       String sql = "SELECT"+ sqlSerch; 
       ResultSet rs = stmt.executeQuery(sql); 

       //Declareer variablen met data uit db 
       int id = rs.getInt("id"); 
       String naam = rs.getString("naam"); 

       System.out.println(id + naam); 

      }catch(Exception e){ 
       System.out.println("Eception"); 
      }   

      System.out.println(" - " + nVal + " - " + inpVal);    
    } 
} 

請告訴我,如果我做錯了什麼。

+0

完整的servlet代碼包括web.xml代碼片段? –

+0

請檢查這個!您的表單操作必須映射到servlet在den web.xml中註冊的url-pattern http://stackoverflow.com/questions/23751965/how-to-map-a-servlet-call-from-a-jsp -page-using-form-action – swinkler

+0

我是否理解你在控制檯中看到「調用db conn函數」和「腳本完成」消息,但沒有「正在運行的函數...」? –

回答

1

因此對於那些感興趣的, 問題不是我的代碼,而是eclipse。 它正在運行我的項目的舊版本,而新的一個被保存。 謝謝你的時間溫柔(wo)男人

+0

糟糕!這就是爲什麼我支付JRebel ;-) –

+0

是的,我不想支付:s 哈哈我只是一名學生 – Casper

+0

我只是想給「解決方案」什麼對我有用, 但不會發生再次。 – Casper