M
M
Maks Burkov2016-08-08 19:46:59
Java
Maks Burkov, 2016-08-08 19:46:59

Passing JSON through Postman and debugging the request to the server, why doesn't it come out?

Hello!
I'm trying to send a JSON date via Postman to a server. The following happens.
When starting the web server itself, Tomcat initializes the filter.
When requested, doFilter is displayed in the filter, what happens to destroy, why is it not called?
Register class: The print is not output to the console, that is, the program did not launch this class, why?
Therefore, I can not transfer data to the MySql server, if anyone can explain the process itself, I will be grateful!
Register example, no console output.

@Path("/register")
     public class Register extends HttpServlet{


     private static final long serialVersionUID = 1068653210561068853L;
     Connection con;

     @POST
     @Consumes({MediaType.APPLICATION_JSON})
     public void Registering(@Valid User user){

    ConnectionPool con_pool = ConnectionPool.con_instance;

    String register = "INSERT INTO users(nickname,first_name,last_name,password,type,email)values(?,?,?,?,?,?)";

    PreparedStatement pre;

    try {

        pre = con.prepareStatement(register);
        pre.setString(1,user.getNickName());
        pre.setString(2,user.getFirstName());
        pre.setString(3,user.getLastName());
        pre.setString(4,user.getPassword());
        pre.setString(5,user.getType());
        pre.setObject(6,user.getEmail());
        pre.execute();

        **System.out.println("In the register method");**


    }catch(com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException e){

        throw new DuplicateUser("Such user already exists in database");

    }catch (SQLException e1) {

        throw new DataBaseError("You have some problems that need to be fixed with your database..");
    }
    finally{

    try {
        con_pool.returnConnection(con);
    } catch (CouponSystemException e1) {
        throw new DataBaseError("You have some problems that need to be fixed with your database..");
    }

    try {
        con.close();
    } catch (SQLException e) {

        throw new DataBaseError("You have some problems that need to be fixed with your database..");
      }
     }      
    }
    }

Filter example, output only doFilter and init when starting the server.
public class RegisterFilter implements Filter{

@Override
public void destroy() {

    System.out.println("In the destroy method");

}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {


    HttpServletResponse httpResponse = (HttpServletResponse)res;

    httpResponse.setHeader("Access-Control-Allow-Origin", "http://localhost");
    httpResponse.setHeader("Access-Control-Allow-Methods", "POST");
    httpResponse.setHeader("Access-Control-Max-Age", "86400");
    httpResponse.setHeader("Content-Language", "en-US");
    httpResponse.setContentType("application/json;charset=utf-8");

    System.out.println("In the do filter method");

}

@Override
public void init(FilterConfig config) throws ServletException {

    System.out.println("In the init method");

}

Example of output to the console:
7d5d55286ab04a85be6a79167427a657.png743426943e6f487fa4d91d7f02edad48.png
<servlet>
  <servlet-name>Register</servlet-name>
  <servlet-class>coupon.register.Register</servlet-class>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>Register</servlet-name>
  <url-pattern>/register/posting</url-pattern>
  </servlet-mapping>
 
  
  <!--  <filter>
  <filter-name>Register</filter-name>
  <filter-class>coupon.filters.RegisterFilter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>Register</filter-name>
  <url-pattern>/register/posting</url-pattern>
  </filter-mapping> 
 -->
  
  <security-constraint>
  <web-resource-collection>
  <web-resource-name>RegisterPosting</web-resource-name>
  <url-pattern>/register/posting</url-pattern>
  <http-method>POST</http-method>
  </web-resource-collection>
  
  <user-data-constraint>
  <transport-guarantee>CONFIDENTIAL</transport-guarantee>
  </user-data-constraint>
  
  
  </security-constraint>
  
   <!-- Registration -->
   
   <session-config>
    <session-timeout>30</session-timeout>
    <cookie-config>
      <name>SESSIONID</name>
    </cookie-config>
  </session-config>

Решение!
@GET
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    Post calling! 
  }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#algooptimize #bottize, 2016-08-08
@user004

init and destroy are not called each time, unlike dofilter
Filters must be called in a chain.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question