我想制作一个 servlet,它将使用我的 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);
}
}