java servlets - kvis

25
Java Servlets Dr. Ferdin Joe John Joseph

Upload: others

Post on 20-Oct-2021

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java Servlets - KVIS

Java ServletsDr. Ferdin Joe John Joseph

Page 2: Java Servlets - KVIS

Create Project• Open Netbeans IDE, Select File -> New Project

Page 3: Java Servlets - KVIS

Select Java Web -> Web Application, then click on Next

Page 4: Java Servlets - KVIS

Give a name to your project and click on Next,

Page 5: Java Servlets - KVIS

and then, Click Finish

Page 6: Java Servlets - KVIS

• The complete directory structure required for the Servlet Application will be created automatically by the IDE.

Page 7: Java Servlets - KVIS

To create a Servlet, open Source Package, right click on default packages -> New -> Servlet.

Page 8: Java Servlets - KVIS

Give a Name to your Servlet class file,

Page 9: Java Servlets - KVIS
Page 10: Java Servlets - KVIS

Now, your Servlet class is ready, and you just need to change the method definitions and you will good to go.

Page 11: Java Servlets - KVIS

Write code

Page 12: Java Servlets - KVIS

Create an HTML file, right click on Web Pages -> New -> HTML

Page 13: Java Servlets - KVIS

• Give it a name. It is recommend to name it index, because browser will always pick up the index.html file automatically from a directory. Index file is read as the first page of the web application.

Page 14: Java Servlets - KVIS
Page 15: Java Servlets - KVIS

Modify the html file

Page 16: Java Servlets - KVIS

• Edit web.xml file. In the web.xml file you can see, we have specified the url-pattern and the servlet-name, this means when hello url is accessed our Servlet file will be executed.

Page 17: Java Servlets - KVIS

Add the welcome file tag

Page 18: Java Servlets - KVIS

Run your application, right click on your Project and select Run

Page 19: Java Servlets - KVIS

Click on the link

Page 20: Java Servlets - KVIS

voilà

Page 21: Java Servlets - KVIS

Servlet Request

• It is used to send a value to another web page and use for manipulation for operations in the landing page

Page 22: Java Servlets - KVIS

Modify index.html

<form method="post" action="check">Name <input type="text" name="user" ><input type="submit" value="submit"></form>

Page 23: Java Servlets - KVIS

Modify web.xml

<servlet><servlet-name>check</servlet-name><servlet-class>MyServlet</servlet-class>

</servlet><servlet-mapping>

<servlet-name>check</servlet-name><url-pattern>/check</url-pattern>

</servlet-mapping>

Page 24: Java Servlets - KVIS

Modify MyServlet.javaimport java.io.*;import javax.servlet.*;import javax.servlet.http.*;

public class MyServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();try {

String user=request.getParameter("user");out.println("<h2> Welcome "+user+"</h2>");

} finally { out.close();

}}

}

Page 25: Java Servlets - KVIS

Class Exercise

• Go to w3schools.com• Browse through the html tags and try to modify the servlet page with

other tags available.