Biblioteca Java - Rev 33
Subversion Repositories:
(root)/Courses and labs samples/SCD/Video_tutorials_servlets/WebPersoana/src/java/com/utcn/aut/servlet/PersoanaServlet.java @ 34
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.utcn.aut.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author mihai
*/
public class PersoanaServlet extends HttpServlet {
HashMap<String,String> persoane = new HashMap<>();
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String nume = request.getParameter("nume");
System.err.println(nume);
String raspuns;
if(nume==null)
raspuns = "Parametrul nume nu a fost specificat.";
else{
if(persoane.containsKey(nume))
raspuns = "Varsta persoanei este="+persoane.get(nume);
else
raspuns = "Persoana nu a fost gasita.";
}
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet AdunaServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>"+raspuns+"</h1>");
out.println("</body>");
out.println("</html>");
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String op1 = request.getParameter("nume");
String op2 = request.getParameter("varsta");
persoane.put(op1, op2);
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet AdunaServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> Persoana a fost adaugata</h1>");
out.println("<h1> Numar total de persoane:"+persoane.size()+"</h1>");
out.println("</body>");
out.println("</html>");
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.utcn.aut.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author mihai
*/
public class PersoanaServlet extends HttpServlet {
HashMap<String,String> persoane = new HashMap<>();
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String nume = request.getParameter("nume");
System.err.println(nume);
String raspuns;
if(nume==null)
raspuns = "Parametrul nume nu a fost specificat.";
else{
if(persoane.containsKey(nume))
raspuns = "Varsta persoanei este="+persoane.get(nume);
else
raspuns = "Persoana nu a fost gasita.";
}
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet AdunaServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>"+raspuns+"</h1>");
out.println("</body>");
out.println("</html>");
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String op1 = request.getParameter("nume");
String op2 = request.getParameter("varsta");
persoane.put(op1, op2);
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet AdunaServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> Persoana a fost adaugata</h1>");
out.println("<h1> Numar total de persoane:"+persoane.size()+"</h1>");
out.println("</body>");
out.println("</html>");
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}