Calling a JSP from a Servlet. To call a JSP from a Servlet, write the following code into the Servlet:
public void doPost(HttpServletRequest request, HttpServletResponse response) {
...
try {
String jsp2Call=/jsp/myJsp.jsp';
getServletConfig().getServletContext().getRequestDispatcher(
jsp2Call).forward(request, response); } catch (Exception e) {.....}
...
}
21 junio 2004
20 junio 2004
Primers passos amb Tomcat 4.1.24
Abans de res, i per a poder compilar, hem de posar el servlet.jar que ve en TOMCAT_HOME/common/lib en el classpath que tinguem definit, o bé copiar-lo en JAVA_HOME/jre/lib/ext. A partir d'ara podrem compilar.
Per comprobar que funciona:
Fitxer HTML de prova:
-----------------------------------------------------
------------------------------------------------------
el copiem a TOMCAT_HOME/examples/hola.html
Servidor
------------------------------------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class hola extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String nom = req.getParameter("nom");
out.println("Hi, "+ nom + "!.");
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}
-------------------------------------------------------
Compilem amb javac hola.java i obtenim hola.class. Copiem el hola.class a TOMCAT_HOME/examples/WEB-INF/classes.
Cridem des d'un navegador a:
http://127.0.0.1:8080/examples/hola.html
omplim el nom i cliquem a POST.
Si obtenim quelcom com:
Hi, nom!
Anem bé!
Per comprobar que funciona:
Fitxer HTML de prova:
-----------------------------------------------------
Nom:
------------------------------------------------------
el copiem a TOMCAT_HOME/examples/hola.html
Servidor
------------------------------------------------------
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class hola extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String nom = req.getParameter("nom");
out.println("Hi, "+ nom + "!.");
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}
-------------------------------------------------------
Compilem amb javac hola.java i obtenim hola.class. Copiem el hola.class a TOMCAT_HOME/examples/WEB-INF/classes.
Cridem des d'un navegador a:
http://127.0.0.1:8080/examples/hola.html
omplim el nom i cliquem a POST.
Si obtenim quelcom com:
Hi, nom!
Anem bé!
Suscribirse a:
Entradas (Atom)