how to redirect the user from a servlet to a new html file inside my web project based on an input parameter? -
i want develop employee management system user choose table inside database ,myservlet chosen button user redirected html page choose action on table. failed redirect user using response.sendredirect("empactions.html") help??
here myfirst html page choose table database:
<form action="choosetable" method="post"> <h1>choose table: </h1> <input type="button" value="employees" name="emp"> <input type="button" value="managers" name="mng"> <input type="button" value="departments" name="deprt"> </form>
and here's firstmainservelet.java redirect html page : package com.example;
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 firstmainservelet extends httpservlet { private static final long serialversionuid = 1l; protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string e = request.getparameter("button"); if(e.equals("employees")) { response.sendredirect("empactions.html"); system.out.println(" selected action "+e); } else if(e.equals("managers")) { response.sendredirect( "mngactions.html"); system.out.println(" selected action "+e); } else if(e .equals("departments")) { system.out.println(" selected action "+e); response.sendredirect( "dptactions.html"); } } }
web.xml file
<?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="webapp_id" version="2.5"> <display-name>management system</display-name> <servlet> <description></description> <display-name>firstservlet</display-name> <servlet-name>firstservlet</servlet-name> <servlet-class>com.servlet.firstmainservelet</servlet-class> </servlet> <servlet-mapping> <servlet-name>firstservlet</servlet-name> <url-pattern>/choosetable </url-pattern> </servlet-mapping> </web-app>
assuming war file has following structure:
- your_project.war ----- meta-inf -------- manifest.mf ----- web-inf -------- classes -------- lib -------- web.xml ----- pages -------- empactions.html -------- mngactions.html -------- dptactions.html
then need following code redirect correct html page:
if(e.equals("employees")) { response.sendredirect(request.getcontextpath() + "/pages/empactions.html"); system.out.println(" selected action " + e); }
Comments
Post a Comment