0% found this document useful (0 votes)
28 views2 pages

Servlet: Suivie Une Session

This document contains code snippets for three different servlets: 1) A NomServlet that handles GET and POST requests and writes responses 2) An HttpSessionServlet that tracks the number of times a page has been visited using a session attribute 3) A DataBaseServlet that connects to a MySQL database, executes a query, and writes results to the response

Uploaded by

Med Hamdani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

Servlet: Suivie Une Session

This document contains code snippets for three different servlets: 1) A NomServlet that handles GET and POST requests and writes responses 2) An HttpSessionServlet that tracks the number of times a page has been visited using a session attribute 3) A DataBaseServlet that connects to a MySQL database, executes a query, and writes results to the response

Uploaded by

Med Hamdani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Servlet

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("/NomServlet")
public class NomServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public NomServlet() {
super(); }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.getWriter().append("Served at: ").append(request.getContextPath());}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
doGet(request, response);
response.setContentType("Text/Plain");
PrintWriter out = response.getWriter();}}

Suivie une Session


public class HttpSessionServlet extends HttpServlet { protected void doGet(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/plain");
PrintWriter out = res.getWriter();
HttpSession session = req.getSession();
Integer count = (Integer)session.getAttribute("count");
if (count == null)
count = new Integer(1);
else count = new Integer(count.intValue() + 1);
session.setAttribute("count", count);
out.println("Vous avez visité cette page " + count + " fois."); } }
Servlet qui interroge une base de données mySQL
public class DataBaseServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection ma_connexion = DriverManager.getConnection("jdbc:derby://localhost:1527/PersonDB");
Statement mon_statement = ma_connexion.createStatement();
ResultSet mon_resultat = mon_statement.executeQuery("SELECT NAME, FIRSTNAME, OLDYEAR FROM
PERSON");
while (mon_resultat.next()) {
out.print("Nom : " + mon_resultat.getString(1)); out.print(" Prénom : " + mon_resultat.getString(2));
out.println(" Age : " + mon_resultat.getInt(3)); out.println("<br>"); } }
catch(SQLException e) { ...}}

You might also like