从Servlet调用特定方法

时间:2016-05-11 13:56:12

标签: javascript java jquery servlets

经过多次学习,我能够完成一个基本的中间层体系结构,并在javascript中调用servlet中的Java方法,将一个变量从javascript \ jquery传递给该方法,并将数据从java方法返回给客户端。我有(我希望是)在我的方式中最后一件事,那就是调用一个特定的方法。目前,我只能在javaclass中调用doGet()和doPost()方法,所以如果我想添加一个新函数,我必须添加一个全新的servlet。如果我想添加一个新函数,我将如何以与调用doGet()类似的方式调用它?我的意思是,我确信我可以使doGet()方法仅仅是一个基于其中一个参数调用其他方法的解析器,但我怀疑这是最佳实践。

这是我的代码:

的index.jsp:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Test</title>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
       function init(){
            $.get("http://localhost:8080/Test/SomeServlet", function(responseText) {   // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text...
                $("#someselect").html(responseText);           // Locate HTML DOM element with ID "somediv" and set its text content with the response text.
            });
        }

        function getSS(){
            var s = $("#someselect").val();
             $.get("http://localhost:8080/Test/SS?name=" + s, function(responseText) {   // Execute Ajax GET request on URL of "someservlet" and execute the following function with Ajax response text...
                $("#SS").html(responseText);           // Locate HTML DOM element with ID "somediv" and set its text content with the response text.
            });
        }
    </script>
</head>
<body onload="init()">

        <select id="someselect" onchange="getSS()"></select>
        <div>
        The SS of this person is: <span id="SS"></span>
        </div>
    </body>
</html>

SomeServlet.java:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static javax.ws.rs.client.Entity.text;

@WebServlet("/someservlet/*")
public class SomeServlet extends HttpServlet {
    private static Statement stmt = null;
    private static ResultSet rs = null;
    private static Connection con = null;


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet SomeServlet</title>");            
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet SomeServlet at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String s22;
try {
       Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

      String connectionUrl = "jdbc:sqlserver://myHost:1433;databaseName=myDB;user=username;password=password";
      con = DriverManager.getConnection(connectionUrl);
        stmt = con.createStatement();
        Statement stmt = null;
        ResultSet rs = null;
        //this is where my sql statement would go, but stack overflow won't accept it for some reason, I keep getting an error and it's this line that causes it when submitting the question.
        stmt = con.createStatement();
        rs = stmt.executeQuery(SQL);
        s22 = "<select>";
        while (rs.next()) {
           s22 = s22 + "<option>"+ rs.getString("first_name") + "</option>";
        }
        s22 = s22 + "</select>";
     } catch (Exception e) {
         s22 =  "Got an exception! " + e.getMessage();
      }


 response.setContentType("text/plain");  
response.setCharacterEncoding("UTF-8"); 
response.getWriter().write(s22);

}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

0 个答案:

没有答案