root/SPARQLBuilderWWW/src/java/org/biohackathon/SPARQLBuilder/www/PLServlet.java @ 188

リビジョン 175, 4.3 KB (コミッタ: atsuko, 10 年 前)

HttpSession? でデータ共有

行番号 
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6
7package org.biohackathon.SPARQLBuilder.www;
8
9import java.io.IOException;
10import java.io.PrintWriter;
11import javax.servlet.ServletException;
12import javax.servlet.annotation.WebServlet;
13import javax.servlet.http.HttpServlet;
14import javax.servlet.http.HttpServletRequest;
15import javax.servlet.http.HttpServletResponse;
16import javax.servlet.http.HttpSession;
17import org.biohackathon.SPARQLBuilder.OWL.*;
18
19/**
20 *
21 * @author atsuko
22 */
23@WebServlet(name = "PLServlet", urlPatterns = {"/plist"})
24public class PLServlet extends HttpServlet {
25
26    private static final String FILENAME = "cdata/";   
27   
28    /**
29     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
30     * methods.
31     *
32     * @param request servlet request
33     * @param response servlet response
34     * @throws ServletException if a servlet-specific error occurs
35     * @throws IOException if an I/O error occurs
36     */
37    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
38            throws ServletException, IOException {
39        response.setContentType("text/html;charset=UTF-8");
40        try (PrintWriter out = response.getWriter()) {
41            /* TODO output your page here. You may use following sample code. */
42            out.println("<!DOCTYPE html>");
43            out.println("<html>");
44            out.println("<head>");
45            out.println("<title>Servlet PLServlet</title>");           
46            out.println("</head>");
47            out.println("<body>");
48            out.println("<h1>Servlet PLServlet at " + request.getContextPath() + "</h1>");
49            out.println("</body>");
50            out.println("</html>");
51        }
52    }
53
54    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
55    /**
56     * Handles the HTTP <code>GET</code> method.
57     *
58     * @param request servlet request
59     * @param response servlet response
60     * @throws ServletException if a servlet-specific error occurs
61     * @throws IOException if an I/O error occurs
62     */
63    @Override
64    protected void doGet(HttpServletRequest request, HttpServletResponse response)
65            throws ServletException, IOException {
66        //processRequest(request, response);
67        response.setContentType("application/json;charset=UTF-8");
68        PrintWriter out = response.getWriter();
69        String ep = request.getParameter("ep");
70        String st = request.getParameter("startclass");
71        String en = request.getParameter("endclass");
72       
73        HttpSession session = request.getSession();
74        QueryPathGenerator qpg = (QueryPathGenerator)session.getAttribute("qpg");
75        if ( qpg == null ){
76        //QueryPathGenerator
77                qpg = new QueryPathGenerator(ep);
78        }
79        SClass[] classes = qpg.getClasses(null);
80        Path[] paths = qpg.getPaths(st, en, true);
81        String jsonstr = "{\"paths\":[";
82        for(int i = 0; i< paths.length; i++){
83            if (i > 0 && paths[i] != null){
84                jsonstr += ",";
85            }
86            //jsonstr += paths[i].toJSONString2();
87            if ( paths[i] == null ){
88                continue;
89                //System.out.println("here");
90            }
91            jsonstr += paths[i].toJSONString3(classes);
92        }
93        jsonstr += "]}";
94        System.out.println("JSON:");
95        System.out.println(jsonstr);
96        out.print(jsonstr);
97        //List<Path>
98        //session.setAttribute("pathlist", pathlist);
99    }
100
101    /**
102     * Handles the HTTP <code>POST</code> method.
103     *
104     * @param request servlet request
105     * @param response servlet response
106     * @throws ServletException if a servlet-specific error occurs
107     * @throws IOException if an I/O error occurs
108     */
109    @Override
110    protected void doPost(HttpServletRequest request, HttpServletResponse response)
111            throws ServletException, IOException {
112        processRequest(request, response);
113    }
114
115    /**
116     * Returns a short description of the servlet.
117     *
118     * @return a String containing servlet description
119     */
120    @Override
121    public String getServletInfo() {
122        return "Short description";
123    }// </editor-fold>
124
125}
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。