私の Web サービス ( 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);
}
}