从servlet调用Java函数,函数将不会执行

时间:2015-09-24 09:11:57

标签: java servlets

我正在JSP和Java(servlet)中创建一个简单的Maven Web应用程序。我的webapp的功能是在数据库中搜索其中注册的人员。它搜索ID或名称。所以我有一个带有将数据发送到我的servlet的表单的JSP,在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);              
    }
}

如果我做错了,请告诉我。

1 个答案:

答案 0 :(得分:1)

那么对于那些感兴趣的人, 问题不是我的代码而是eclipse。 当新项目保存时,它正在运行我的项目的旧版本。 谢谢你的温柔(男)男人