changing HttpServletRequest with default class

时间:2017-10-12 10:02:56

标签: java mysql servlets

i want to write without request.getParameter(), and it is confusing me. i tried to call class in servlet that already has values to insert in database without getting attributes from database. Could you help me, what type of error is there? (here is my full code with servlet and web.xml)

  1. Servletik.class

     public class Servletik extends HttpServlet {
     private static final long serialVersionUID = 1L;
    
    
     public Servletik() {
     super();}
    
     protected void doPost(UserDao request, HttpServletResponse 
        response) throws ServletException, IOException {
        String fname = null;
        String lname = null;
        String dob = null;
        String email = null;
        String uid = null;
        UserDao.addUser(fname, lname, dob, email, uid);
    
     }}
    
  2. DBConnect.java

        public class DBconnect {
        private static Connection connection = null;
    
        public static Connection getConnection() {
    
    
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection =  DriverManager.getConnection("jdbc:mysql://localhost:3306/UserDB", "root","" );
    
        } 
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;}}
    
  3. UserDAO

        public class UserDao {
    
        private static Connection connection;
    
        public UserDao() {
        connection = (Connection) DBconnect.getConnection();}
    
        public static void addUser(String fname, String lname, String dob, String email, String uid) {
        try {
        PreparedStatement preparedStatement = ((java.sql.Connection) connection).prepareStatement("insert into users(firstname,lastname,dob,email) values (?, ?, ?, ? );");
    
        preparedStatement.setString(1, fname);
        preparedStatement.setString(2, lname);
        preparedStatement.setString(3, dob);
        preparedStatement.setString(4, email);
        preparedStatement.executeUpdate();
    
        } catch (SQLException e) {
        e.printStackTrace();}}}
    
  4. web.xml

         <welcome-file-list>
         <welcome-file>index.html</welcome-file>
         <welcome-file>index.htm</welcome-file>
         <welcome-file>index.jsp</welcome-file>
         <welcome-file>default.html</welcome-file>
         <welcome-file>default.htm</welcome-file>
         <welcome-file>default.jsp</welcome-file>
         </welcome-file-list>
          <servlet>
         <description></description>
         <display-name>Servletik</display-name>
         <servlet-name>Servletik</servlet-name>
         <servlet-class>nurzhan.servlet.Servletik</servlet-class>
         </servlet>
         <servlet-mapping>
         <servlet-name>Servletik</servlet-name>
         <url-pattern>/Servletik</url-pattern>
         </servlet-mapping>
         </web-app>
    

0 个答案:

没有答案