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
Java Division by Zero Doesnt Throw an Arithmeticexception - Why
How to Retrieve Image from Project Folder
Spring MVC - How to Return Simple String as JSON in Rest Controller
Spring Data JPA - "No Property Found for Type" Exception
Updating UI from Different Threads in Javafx
Delete Item from Array and Shrink Array
String Comparison and String Interning in Java
Different Types of Thread-Safe Sets in Java
Java: How to Import a Jar File from Command Line
When Should We Create Our Own Java Exception Classes
Why Equal Operator Works for Integer Value Until 128 Number
Code for Changing the Color of Subtasks in Gantt Chart
Spring Cache @Cacheable - Not Working While Calling from Another Method of the Same Bean
Lambda Expression to Convert Array/List of String to Array/List of Integers
How to Generate Cdata Block Using Jaxb
How to Inject an Object into Jersey Request Context
MAC Osx - Illegalstateexception: the Driver Is Not Executable: