0

I want to make an servlet that will use my web service, (String flag(string country)), receives a name of a country and returns the URL with the image of the country! The function is working good, it's receiver properly and is returning the right data:

Returns the string -> "http://www.oorsprong.org/WebSamples.CountryInfo/Images/Poland.jpg", I would like to redirect my browser to this URL.

For that, it should be sent with JSP, XML code, I tried an example from this tutorial 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>

and it works when I put directly in my JSP input code.

But on the JSP for the output i can't redirect the flag's URL, because the code doesn't recognize it as something to execute, and it shows as a result.

And it's just prints the lines I want to be executed.

And here is the code I used:

/*
 * 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);
    }


}
pb2q
  • 58,613
  • 19
  • 146
  • 147
Z.Mike
  • 11
  • 3
  • The evilness of NetBeans strikes again with `processRequest` method on `doGet` and `doPost` =\ – Luiggi Mendoza Mar 31 '13 at 02:44
  • 1
    You have two errors here: 1. Using scriptlets when is highly discouraged: [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files). 2. Printing HTML code directly from your servlet to the `HttpServletResponse`, note that this is pure HTML so scriptlets inside it won't be executed. Your best bet would be forwarding from your servlet to a page that handles the `String` returned from your web service and shows the image. You can find a start example in [StackOverflow Servlets wiki](http://stackoverflow.com/tags/servlets/info) – Luiggi Mendoza Mar 31 '13 at 02:50

0 Answers0