访问servlet时出现404错误(ajax GET请求)

时间:2013-05-23 10:15:29

标签: ajax jsp jquery servlets get

发送ajax请求时,我一直收到404错误。以下网址返回404。 本地主机:8091 /应用/ GetQuestion QID = 1 代码中的错误是什么? HTML。附加了web.xml和servlet代码。请帮我弄清楚出了什么问题!

遵循本教程也会给我一个404错误 http://www.srccodes.com/p/article/3/Tomcat-Hello-World-Servlet-using-Eclipse-IDE

的index.html

    $(document).ready(function() {

            $('#takeSurvey').click(function() {

                $.ajax(
                           {
                              type:'GET',
                              url:'/App/GetQuestion',
                              data:"qid=1",
                              success: function(data){
                                alert('successful');
                              }
                           }
                        );                              
            });
        });

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>App</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>GetQuestion</servlet-name>
    <servlet-class>GetQuestion</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>GetQuestion</servlet-name>
    <url-pattern>/GetQuestion</url-pattern>
  </servlet-mapping>
</web-app>

Servlet - GetQuestion.java

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
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 com.google.gson.Gson;
/**
 * Servlet implementation class GetQuestion
 */
@WebServlet("/GetQuestion")
public class GetQuestion extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetQuestion() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String connectionURL = "jdbc:mysql://localhost:3306/survey";
        Connection connection=null;

        ResultSet rs;
        List<String> list = new ArrayList<String>();

        try{
                System.out.println("Entered servlet");
                Class.forName("com.mysql.jdbc.Driver");
                // Get a Connection to the database
                connection = DriverManager.getConnection(connectionURL, "root", "root123"); 
                System.out.println("Connection Successful");
                Statement s = connection.createStatement();

                 String sql="select qtext from question where qid=1";
                 rs= s.executeQuery(sql);
                 System.out.println(sql);
                 rs = s.getResultSet();
                 while (rs.next()) {
                      //Add records into data list
                      list.add(rs.getString("qtext"));
                      rs.close();
                      s.close();
                 }
                }
              catch(Exception e){
                System.out.println("Exception is ;"+e);
                }
                String json = new Gson().toJson(list);

                response.setContentType("application/json");
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write(json);
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }
}

0 个答案:

没有答案
相关问题