本帖最后由 lchina1314 于 2011-03-09 10:35:29 编辑

解决方案 »

  1.   

    大家帮我看看封装的json格式和请求的url有什么问题吗?我哪里需要修改吗?
      

  2.   

    URL请求servlet时,地址应该是class文件吧?不过我改了还是404NOT Found。
      

  3.   

    URL 请求 Servlet ?  说说错吧
      

  4.   

    报错在最后面2个图的。404not found,不知道为什么???
      

  5.   

    这样吧,用个SSH 框架  请求的时候 把 url 改下 看行不?  你的服务器我没有用过, 也不知道 Json 请求的路径对不对
      

  6.   

    不过 直接根据 项目路径来请求应该 错了... 又不是  .Net
      

  7.   


    是web.xml没有配置啊,谁帮我看看怎么填写啊,我第一次做这个不知道。前面已经给出文件路径了。
      

  8.   

    servlet-name:viewData
    url-patten:viewData.do
    servlet-name:viewData
    servlet-class:db.ViewDataajax 请求 url:/myvideo/DNSwebs/viewData.do大概是这个样子吧
      

  9.   

    你的2次URL请求的路径都不一样
    第一次是完整的网络地址
    第二次出错时只有你的工程内部路径你加上url标签试试
      

  10.   

    device_manage.jsp文件
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <head>
        <base href="<%=basePath%>">
    </head>
    <style>
    html, body {
        margin: 0;
        padding: 0;
        font-size: 100%;
    }
    .ui-jqgrid tr.jqgrow td {
        white-space: normal !important;
        height:auto;
        vertical-align:text-top;
        padding-top:10px;
    }
    </style>
    <SCRIPT type="text/javascript">  
        $(document).ready(function()   
        {   
            $("#gridTable").jqGrid({  
            
    url:'/DNSWebs/servlet/ViewDataServlet',
       mtype:"POST",
       datatype:'json',
                    autowidth : true,   
                    height:'auto',   
                    colNames:['设备ID','设备域名', 'IP地址', '端口号','更新时间','状态'],   
                    colModel:[   
                            {name:'devId',index:'devId', width:120, editable:true},
        {name:'devDomainName',index:'devDomainName', width:120,editable:true},
        {name:'ipAddress',index:'ipAddress', width:80, align:"right"},
        {name:'port',index:'port', width:80,align:"right"},
        {name:'updateTime',index:'updateTime', width:120,sorttype:'date', editrules:{date:true},formatter:'date', datefmt:'d/m/Y'},
        {name:'state',index:'state', width:80, sortable:false}                  
                    ], 
                    jsonReader:{
    root: "rows",   // json中代表实际模型数据的入口  
    page: "page",   // json中代表当前页码的数据  
    total: "total", // json中代表页码总数的数据  
    records: "records", // json中代表数据行总数的数据 
    repeatitems:false 
    },
                    width:2,
                    sortname:'devId',   
                    sortorder:'asc',   
                    viewrecords:true,   
                    rowNum:10,               
                    rowList:[10,20,30], 
                    pager:"#pager",
                    cellEdit:false, 
                    caption: "设备管理",   
                    multiselect: true ,
                    sunGrid:true 
      
       }).navGrid('#pager',{edit:true,add:true,del:true,search:true,refresh:true,view:true});   
     var mydata = [   
                    {devId:'ID0000801940411152210651',devDomainName:'test1',ipAddress:'119.39.97.85',port:'3000',updateTime:'2011-02-27 07:47:52.0',state:'1'},   
                    {devId:'ID0000801940410882100613',devDomainName:'test2',ipAddress:'58.45.131.233',port:'3000',updateTime:'2011-02-26 04:46:24.0',state:'1'},   
                    {devId:'ID0000801940411232310669',devDomainName:'test3',ipAddress:'123.120.26.43',port:'3000',updateTime:'2011-02-23 04:35:26.0',state:'1'},                                  
                    ]; 
            for(var i=0;i<=mydata.length;i++)   
                    $("#gridTable").jqGrid('addRowData',i+1,mydata[i]);
            });  </script>      
    <table id="gridTable" ></table>   
    <div id="pager" class="scroll" style="text-algin:center;"></div> ViewDataServlet.java文件
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    public class ViewDataServlet extends HttpServlet { /**
     * Constructor of the object.
     */
    public ViewDataServlet() {
    super();
    } /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
    } /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { doPost(request,response);
    } /**
     * The doPost method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to post.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    System.out.println("Test");
    String driver="com.mysql.jdbc.Driver";
    String url="jdbc:mysql://localhost:3306/pcwebdb";
    String user="root";
    String password="root";
    String sql=null;
    Connection conn=null;
    Statement statement=null;
    ResultSet rs=null;
    try
    {
    conn = DriverManager.getConnection(url,user,password);
    statement = conn.createStatement();
    sql = "select * from tab_dev";
    rs=statement.executeQuery(sql);
    Class.forName(driver);

    if(!conn.isClosed())
    {
    System.out.println("Succeeded connecting to the Database!");
    }
    String dev_id=null;
    String dev_memo=null;
    String dev_OuterIP=null;
    String dev_OuterPort=null;
    String dev_Time_UTC=null;
    String dev_active=null;


    while(rs.next())
    {
    dev_id=rs.getString("dev_id");
    dev_memo=rs.getString("dev_memo");
    dev_OuterIP=rs.getString("dev_OuterIP");
    dev_OuterPort=rs.getString("dev_OuterPort");
    dev_Time_UTC=rs.getString("dev_Time_UTC");
    dev_active=rs.getString("dev_active"); String page="1";
    String rows="1";
    page=request.getParameter("page");
    rows=request.getParameter("rows");
    int totalRecord=1648;
            
            int totalPage = totalRecord%Integer.parseInt(rows) == 0 ?    
                    totalRecord/Integer.parseInt(rows) : totalRecord/Integer.parseInt(rows)+1;  
            try {   
            int index = (Integer.parseInt(page)-1)*Integer.parseInt(rows); 
            int pageSize = Integer.parseInt(rows);            String json = "{total: "+totalPage+", page: "+page+", records: "+totalRecord+", rows: [";   
            for (int i = index; i < pageSize + index && i<totalRecord; i++) {   
                json += "{devId:'"+dev_id+"',devDomainName:'"+dev_memo+"',ipAddress:'"+dev_OuterIP+"',port:'"+dev_OuterPort+"',updateTime:'"+dev_Time_UTC+"',state:'"+dev_active+"'}";   
                if (i != pageSize + index - 1 && i != totalRecord - 1) {   
                    json += ",";   
                }   
            }   
            json += "]}";   
            System.out.println(json); 
            response.setCharacterEncoding("UTF-8");
            response.getWriter().write(json);  
            } catch (Exception ex) 
            {   
             System.out.print("Excep in ViewData when invoke method getData"+ex.getMessage());
            }   

    }         
    rs.close();
    conn.close();
    }
    catch(ClassNotFoundException e)
    {
    System.out.println("Sorry,can't find the Driver");
    e.printStackTrace();
    }
    catch(SQLException e)
    {
    e.printStackTrace();
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    } /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */
    public void init() throws ServletException {
    // Put your code here
    System.out.print("ViewDataServlet");
    }}web.xml文件
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <servlet>
        <description>This is the description of my J2EE component</description>
        <display-name>This is the display name of my J2EE component</display-name>
        <servlet-name>ViewDataServlet</servlet-name>
        <servlet-class>ViewDataServlet</servlet-class>
      </servlet>
      
      <servlet-mapping>
        <servlet-name>ViewDataServlet</servlet-name>
        <url-pattern>/servlet/ViewDataServlet</url-pattern>
      </servlet-mapping>
     <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
     </welcome-file-list>
     <login-config>
      <auth-method>BASIC</auth-method>
     </login-config>
     
     </web-app>Error 404