I have designed a simple servlet
, but it not working Instead of executing, it is getting downloaded through browser window.
When i execute the code, the HTML page runs OK, but after I submit form data the servlet gets downloaded as shown without servlet execution..
I tried so many times modifying the code and also searched I did not find this anywhere, so any help is appreciated
Please see the screenshots I have attached:
Image1(https://i.stack.imgur.com/YWXzp.jpg) Image2(https://i.stack.imgur.com/WMCwc.jpg)
//CLASS file
package com.pkg;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//@WebServlet("/SltClass")
public class SltClass extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("HTML/text");
String userNameVar = request.getParameter("userName");
String passWordVar = request.getParameter("passWord");
PrintWriter out = response.getWriter();
out.println("hello user :" +userNameVar);
}}
HTML
</head>
<body>
<div>TODO write content</div>
<form action="Welcome" method="post">
<input type="text" name="userName" />
<input type="text" name="passWord" />
<input type="submit" value="Submit it" />
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Sname</servlet-name>
<servlet-class>com.pkg.SltClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Sname</servlet-name>
<url-pattern>/Welcome</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>