如何将JSP浏览器重定向到Servlet中的给定URL

时间:2013-03-30 18:45:03

标签: jsp servlets webservice-client

我想创建一个将使用我的Web服务的servlet(String flag(string country)),接收一个国家的名称并返回带有该国家图像的URL!该功能运行良好,接收器正确并返回正确的数据:

返回字符串 - > “http://www.oorsprong.org/WebSamples.CountryInfo/Images/Poland.jpg”, 我想将浏览器重定向到此URL。

为此,它应该与JSP,XML代码一起发送,我在本教程中尝试了一个例子http://www.tutorialspoint.com/jsp/jsp_page_redirect.htm

<%@ page import="java.io.*,java.util.*" %>
<html>
<head>
<title>Page Redirection</title>
</head>
<body>
<center>
<h1>Page Redirection</h1>
</center>
<%
   // New location to be redirected
   String site = new String("http://www.photofuntoos.com");
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setHeader("Location", site); 
%>
</body>
</html>

当我直接放入我的JSP输入代码时它就可以工作了。

但是在输出的JSP上,我无法重定向标志的URL,因为代码不会将其识别为要执行的内容,并且它会显示为结果。

它只是打印我想要执行的行。

以下是我使用的代码:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package flag.servlet;

import flag_c.FlagCountry_Service;
import java.io.IOException;
import java.io.PrintWriter;
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 javax.xml.ws.WebServiceRef;

/**
 *
 * @author I
 */
@WebServlet(name = "Flag_Servlet", urlPatterns = {"/Flag_Servlet"})
public class Flag_Servlet extends HttpServlet {
    @WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/WS2_Flag/Flag_Country.wsdl")
    private FlagCountry_Service service;
    private String TextArea1;


    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
           out.println("<!DOCTYPE html>");

            String TextArea = request.getParameter("TextArea1");

            //Initialize WS operation arguments
            java.lang.String bodyText = TextArea;
            String flag_url = flag(bodyText);  
            //Until here it's ok, receives the string with the country, and returns
           //the flag's url in a string
           //Now i want to out.println my xml, with the code to redirect the flag's url

            out.println("<html>");
            out.println("<head>");
            out.println("<title><font color ='red'> Servlet Flag_Servlet </font></title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet Flag_Servlet at " + request.getContextPath() + "</h1>");


           //I Added code here , flag_url as my flag's url..........
            out.println("<%= " + flag_url +" %>"); // Here is my answer flag_url
            out.println(" "                        // Here the code for the xml output
                    + "<%\n" +
"                    // New location to be redirected\n" +
"                    response.setStatus(response.SC_MOVED_TEMPORARILY);\n" +
"                    response.setHeader(\"Location\","+ flag_url +"\");\n" +
"                   %>");
           //.......................................................

            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    //Returns the country's flag "on" a url
    //This is the service from the Web Service

    private String flag(java.lang.String country) {
        flag_c.FlagCountry port = service.getFlagCountryPort();
        return port.flag(country);
    }


}

0 个答案:

没有答案