Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.*;
- //import javax.servlet.http.HttpServletRequest;
- //import javax.servlet.http.HttpServletResponse;
- import java.sql.*;
- import java.util.ArrayList;
- /**
- *
- * @author Owner
- */
- public class Update_Servlet extends HttpServlet {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- HttpSession mySession = request.getSession();
- ArrayList uNames = (ArrayList)mySession.getAttribute("uNames");
- String UName = request.getParameter("username");
- String PWord = request.getParameter("password");
- String submit = request.getParameter("cmdSubmit");
- String origUName="";
- String error = null;
- boolean update = false;
- if(submit != null) //user clicked submit button
- {
- //retrieve session variable
- origUName = (String) mySession.getAttribute("origUName");
- if(UName.trim().length() > 0 && PWord.trim().length() > 0)
- {
- if(UName.matches("^[A-za-z0-9]+$") && PWord.matches("^[A-Za-z0-9]+$"))
- {
- if(uNames != null)
- {
- if(!UName.equals(origUName))
- {
- if(!uNames.contains(UName))
- {
- update = true;
- }
- else
- {
- error = "Primary key violation...("+ UName + ")...Enter new username!";
- }
- }
- else
- {
- update = true;
- }
- }
- else
- {
- error = "Cookes must be enabled";
- }
- }
- else
- {
- //display error message
- error = "Fields contain invalid characters!.. re enter data!";
- }
- }
- else
- {
- //display error message
- error = "Fields must be completed!";
- }
- }
- else // if else is true then the page is loaded for the first time...capture original primary key value
- {
- // create session variable
- mySession.setAttribute("origUName", UName);
- }
- if(update)
- {
- String dbURL = "jdbc:odbc:dbJDBC1";
- String sql = "UPDATE tUsers SET username = '" + UName + "',password ='" + PWord + "' WHERE username = '" + origUName + "'";
- try
- {
- Connection conn = DriverManager.getConnection(dbURL);
- Statement s = conn.createStatement();
- s.executeUpdate(sql);
- s.close();
- conn.close();
- response.sendRedirect("Admin_Servlet");
- }
- catch(SQLException ex)
- {
- error = ex.getMessage();
- }
- }
- try {
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet Update_Servlet</title>");
- out.println("</head>");
- out.println("<body style = 'font-family: Verdana, Arial, Sans-Serif'>");
- out.println("<h2 align='center'>Update_Servlet</h2>");
- if(error != null)
- {
- out.println("<p align='center' style='color:red'>" + error + "</p>");
- }
- out.println("<form action='Update_Servlet' method='post'>");
- out.println("<table align='center'>");
- out.println("<tr>");
- out.println("<td align='right'>Username:<input type='text' name='username' value='" + UName +"'/><br /></td>");
- out.println("</tr>");
- out.println("<tr>");
- out.println("<td align='right'>Password:<input type='text' name='password' value='" + PWord +"'/><br /></td>");
- out.println("</tr>");
- out.println ("<tr>");
- out.println("<td align='center' colspan='2'><input type='submit' value='Update Record' name='cmdSubmit' /></td>");
- out.println("</tr>");
- out.println("</table>");
- out.println("</form>");
- out.println("</body>");
- out.println("</html>");
- } finally {
- out.close();
- }
- }
- // <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 {
- processRequest(request, response);
- }
- /**
- * 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 {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement