gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:55:20 24 4
gpt4 key购买 nike

我正在使用 JSP 和 Java(servlet)创建一个简单的 Maven Web 应用程序。我的网络应用程序的功能是通过数据库搜索其中注册的人员。它根据 ID 或姓名进行搜索。因此,我有一个 JSP,其表单将数据发送到我的 servlet,在 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);
}
}

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

最佳答案

所以对于那些感兴趣的人来说,问题不在于我的代码,而在于 Eclipse。它正在运行我的项目的旧版本,而新版本已保存。谢谢您的宝贵时间,绅士们

关于java - 从 servlet 调用 Java 函数,函数不会执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32757552/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com