<Form Action="/Sampleservlet" Giving Me Exception

form action=/sampleServlet giving me exception

When you are using URL in HTML, without leading / they are relative to the current URL (ie current page displayed). With leading / they are relative to the website root :

<form action="/context-path/sampleServlet">

or

<form action="sampleServlet">

will do what you want.

I suggest you to add the context inside the action path dynamically.
Example (in JSP) :

<form action="${pageContext.request.contextPath}/sampleServlet">

With this you will never have to change the path, for example, if you move your file or copy your code, or rename your context!

Calling servlet from a JSP page's form action

You need to create servlet mapping in your web.xml. See here as well.
So in your web.xml define;

<servlet>
<servlet-name>hello</servlet-name>
<servlet-class><package name>.HelloServlet</servlet-class>
</servlet>

Then create mappings (url patterns) for the servlet.

<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/say_hello/*</url-pattern>
</servlet-mapping>

Now in your JSP refernce the servlet like

 <form action="say_hello" method="get">            
<b>Fibonacci Sequence Length </b> <br>
<input type="text" name="fibNum"size="20px" style="font-size:30pt;height:60px" >
<input type="submit" value="submit" style="font-size:30pt;height:60px" > <br>
Value [1-100]<br>
</form>

Configure java servlet xml and html form with packages

Your form should submit to the URL your servlet is mapped to in url-pattern within the servlet-mapping. You need action set to asd/MyClass.

<form action="asd/MyClass" method="get">
<input type="submit"/>
</form>

404 with simple java servlet/html form

Are you sure the url in the form is correct? http://localhost:8080/Lesson41/
If you are using Eclipse, by default it deploys your application as http://localhost:8080/[Your-Project-Name-Here]/Lesson41/

description The requested resource is not available

The URL Pattern in your web.xml is

<servlet-mapping>
<servlet-name>CalculateServlet</servlet-name>
<url-pattern>/CalculateServlet</url-pattern>
</servlet-mapping>

But your jsp submits it to "calculate"

<form action="calculate">

Change it to,

<form action="CalculateServlet">

Configure java servlet xml and html form with packages

Your form should submit to the URL your servlet is mapped to in url-pattern within the servlet-mapping. You need action set to asd/MyClass.

<form action="asd/MyClass" method="get">
<input type="submit"/>
</form>


Related Topics



Leave a reply



Submit