Home > Software engineering >  How to send a user to another webpage. Using Java Servlet?
How to send a user to another webpage. Using Java Servlet?

Time:11-06

I have created an index.jsp file which uploads excel files. The uploader.java file will write t`hat file to a location and then I want to send the user to another webpage where they can enter the sheet number in the excel file. All the mentioned files are running individually without any errors. When I am running the dynamic Web application I created in Eclipse, I am getting the following error:

HTTP Status 404 – Not Found
Type Status Report

Message JSP file 

[/C:/Users/khuha/eclipse-workspace/finalApplication/src/main/webapp/sheetsdetails.jsp] not found

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

Apache Tomcat/8.5.82

I have created a Servlet which has to redirect the user to the webpage sheetsdetails.jsp. following is the code:


package finalApplication;


import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;








public class uploader extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        try {
            ServletFileUpload sf=new ServletFileUpload(new DiskFileItemFactory());

            List<FileItem> multifiles = sf.parseRequest(request);
            //String file="";
            String filename = null;
            for(FileItem item : multifiles)
            {
                
                filename =item.getName();
                
                item.write(new File("C:\\Users\\khuha\\eclipse-workspace\\finalApplication" "\\" filename));


            }
            HttpSession session=request.getSession();

            session.setAttribute("file",filename);

            RequestDispatcher rd=request.getRequestDispatcher("/C:\\Users\\khuha\\eclipse-workspace\\finalApplication\\src\\main\\webapp\\sheetsdetails.jsp");
            rd.forward(request,response );
        }
        catch (Exception ex) {  
            
            
            System.out.println(ex);



        }

    }

}

sheetsdetails.jsp:

<html>
<style>
body{
background: rgb(5,1,28);
background: linear-gradient(180deg, rgba(5,1,28,1) 0%, rgba(116,85,207,1) 67%, rgba(8,2,18,1) 90%);;
color: white;
font-size: 40px;
font-family: "Verdana","sans-seriff";
}
form    {color: white;}
input[type=submit] {
  background-color: transparent;
  border-colour:white ;
  color: cyan;
  
  
 
  margin: 4px 2px;
  cursor: pointer;
}




</style>


<body>

<form action="dataCollect">        
      
<label for="sheetnumber" style="position:absolute; left:415px; top:250px">Enter the sheet number you want to  look in
</label>
<input type="number" id="sheetnumber" name="sheetnumber" value="1" min="1" style="position:absolute; left:690px; top:300px; cursor: pointer;">


 <div style = "position:absolute; left:745px; top:350px; ">
 <input type="submit" name="submit">
 </div>
</form> 

<div style = "position:absolute; left:575px; top:600px; width:50px; height:10px;">
        <img src="finallogo.png" alt="logo" width=375px height=100px/>
      </div>
      
</body>

</html>

What I want to do:

I want to upload a file to the server and then send the user to another webpage where they select the sheet number from the uploaded excel file. Please let me kow if I am using the correct approach to do so.

P.S. this is my first time working with dynamic webapps so any resources where I can learn to send user to a webpage would be highly useful and appreciated.

CodePudding user response:

You should not reference a jsp from disk like this:

/C:\Users\khuha\eclipse-workspace\finalApplication\src\main\webapp\sheetsdetails.jsp

just add:

response.sendRedirect(request.getContextPath()   "/sheetsdetails.jsp")
  • Related