Ad Code

Responsive Advertisement

Ticker

6/recent/ticker-posts

Create a client side HTML web page to input your name from textbox and display “Hello ” on the servlet after clicking on the “Submit” button.

Here we are using the Servlet technology in Java to display the name on the web browser which we will be giving input. 

Here we have used the NetBeans IDE to perform the java program, you can also use the Eclipse IDE.

Java Program -

index.html - 

<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form method ="post" action="hello">
            Name <input type ="text" name="user">
            <input type="submit" value="submit">
        </form>
        <div>TODO write content</div>
    </body>
</html>


web.xml - 

<?xml version="1.0" encoding="UTF-8"?>

<web-app 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"
version="3.1">
    
    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>HelloUser</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>


HelloUser.java - 

package HelloUser;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class HelloUser extends HttpServlet{
    protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String user = request.getParameter("user");
        out.println("<h2> Welcome "+user+"<h2>");
        
    }
    
}


Output - 


Post a Comment

0 Comments

Ad Code

Responsive Advertisement