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.http.*;
- import javax.servlet.ServletException;
- import java.util.ArrayList;
- //import javax.servlet.http.HttpServlet;
- //import javax.servlet.http.HttpServletRequest;
- //import javax.servlet.http.HttpServletResponse;
- import java.sql.*;
- /**
- *
- * @author Owner
- */
- public class Insert_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("txtUsername");
- String PWord = request.getParameter("txtPassword");
- String error = null;
- if(UName != null && PWord != null)
- {
- 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(!uNames.contains(UName))
- {
- // write record to database
- String dbURL = "jdbc:odbc:dbJDBC1";
- String sql = "INSERT INTO tUsers (username,password) VALUES('" + UName + "','" + PWord + "')";
- try
- {
- Connection conn = DriverManager.getConnection(dbURL);
- Statement s = conn.createStatement();
- s.executeUpdate(sql);
- s.close();
- conn.close();
- response.sendRedirect("Admin_Servlet");
- }
- catch(SQLException e)
- {
- }
- }
- else
- {
- error = "Primary key violation....("+ UName + ")...Enter new username!";
- }
- }
- else
- {
- error ="Cookies need to be enabled!";
- }
- }
- else
- {
- error = "Fields contain invalid characters!";
- }
- }
- else
- {
- error="Fields must be completed!";
- }
- }
- try {
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet Insert_Servlet</title>");
- out.println("</head>");
- out.println("<body style = 'font-family: Verdana, Arial, Sans-Serif'>");
- out.println("<h2 align='center'>Insert_Servlet</h2>");
- if(error != null)
- {
- out.println("<p>" + error + "</p>");
- }
- out.println("<form action='Insert_Servlet' method='post'>");
- out.println("<table align='center'>");
- out.println("<tr>");
- out.println("<td align='right'>Username:<input type='text' name='txtUsername' /></td>");
- out.println("</tr>");
- out.println("<tr>");
- out.println("<td align='right'>Password:<input type='text' name='txtPassword' /></td>");
- out.println("</tr>");
- out.println("<tr>");
- out.println("<td align='center' colspan='2'><input type='submit' value='Write Record' /></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