Paramétrage web.xml et index.jsp
Bonjour,
J'utilise NetBeans et j'ai des difficultés dans le paramétrage de ma première Servlet.
Dans index.jsp il faut bien un moment donné que je démarre ma Servlet ?
J'ai spécifié dans web.xml l'existence de ma servlet mais il me manque encore quelque chose.
Fichier WEB.XML
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>MonServlet1</servlet-name>
<servlet-class>MonServlet1.NewClass</servlet-class>
</servlet>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app> |
Index.jsp
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| <%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h2>Hello World!</h2>
</body>
</html> |
MonServlet1.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package MonServlet1;
import javax.servlet.http.*; // HTTP specific servlet stuff
import java.io.*; // Servlets do IO and throw IOExceptions
class Hello extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException
{
String name = request.getParameter("username");
if (name == null)
name = (String)request.getSession().getAttribute("username");
if (name == null) name = "World";
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Helllllo " + name + "!");
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException
{
doGet(request, response);
}
} |