Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The web page that sends parameters to servlet does not return result instead

404 appears

it has been deployed and web xml has the details

The web page is

<html>
    <head>
        <title>Wow</title>        
    </head>
    <body>
        <form  name ="wow" method="GET" action="ThatWas">
            Name : <input type="text" name="name">                        
            <input type="submit" value="That">                        
        </form>        
    </body>
</html>



Servlet
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 ThatWas extends HttpServlet 
{    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
{
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            String name = request.getParameter("name");            
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ThatWas</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("That was good " + name + request.getContextPath());            
            out.println("</body>");
            out.println("</html>");
        }
}


and the xml is

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="file-upload" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">	       
        
        <servlet>
            <servlet-name>ThatWas</servlet-name>
            <servlet-class>ThatWas</servlet-class>
       </servlet>
    
        <servlet-mapping>
		<servlet-name>ThatWas</servlet-name>
                <url-pattern>/ThatWas</url-pattern>
	</servlet-mapping>
        
        <welcome-file-list>
            <welcome-file>That.html</welcome-file>
        </welcome-file-list>
</web-app>


What I have tried:

Code changes and research with internet and books
Posted
Updated 27-Dec-21 22:59pm
v2
Comments
Richard MacCutchan 13-Dec-21 5:08am    
You cannot expect anyone here to guess what your secret code is doing.

1 solution

That code works

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 ThatWas extends HttpServlet 
{    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
{
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            String name = request.getParameter("name");            
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ThatWas</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("That was good " + name );            
            out.println("</body>");
            out.println("</html>");
        }
}
 
Share this answer
 
v3
Comments
Richard MacCutchan 28-Dec-21 6:12am    
"if you put name as What and the file name is That then the result is WhatThat"
Well that is what you tell it to print.
four systems 28-Dec-21 6:16am    
it was cause of request.getContextPath() now it works

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900