Java/Servlets/Redirect — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 06:10, 1 июня 2010
Содержание
Client auto refresh
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class AutoServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
//client browser will request the page every 60 seconds
HttpSession session = request.getSession();
Long times = (Long) session.getAttribute("times");
if (times == null)
session.setAttribute("times", new Long(0));
long temp = 1;
if (times != null)
temp = (times.longValue()) + 1;
if (temp < 5)
response.addHeader("Refresh", "15");
response.setContentType("text/html");
java.io.PrintWriter out = response.getWriter();
out.println("<html><head><title>Client Refresh</title></head><body>");
//More HTML or dynamic content
out.println("You"ve viewed this page " + temp + " times.");
session.setAttribute("times", new Long(temp));
out.println("</body></html>");
} //end doGet
}
Forward Call from a Servlet
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Main extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("/p.jsp");
dispatcher.forward(request, response);
}
}
Redirect Servlet Call to Another URL
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Main extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Redirect call to another url
response.sendRedirect("http://www.jexp.ru");
}
}
Redirect to New Location
/*
* <servlet>
<servlet-name>RedirectNewLocation</servlet-name>
<servlet-class>RedirectNewLocation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RedirectNewLocation</servlet-name>
<url-pattern>/RedirectNewLocation</url-pattern>
</servlet-mapping>
* */
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RedirectNewLocation extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
response.setHeader("Location", "http://www.jexp.ru");
response.setContentType("text/html");
return;
}
}
Servlet redirect
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class RedirectWithLinkServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<html><body>");
out.println("<H1>Java Source and Support</H1><BR>");
out.println("");
out.println("</body></html>");
return;
}
}