search code in servlet to mysql?

后端 未结 1 481
广开言路
广开言路 2021-01-23 11:20

what is searching code in servlet to mysql?

I need simple coding anyone pls help me

i created a register page (Name,Qualification,Country,State) using with Html

1条回答
  •  隐瞒了意图╮
    2021-01-23 11:40

    I create a simple demo project which you mention, i use jstl:core library which can be download from here

    web.xml

        
        
            TestServlet
            
                index.html
                index.htm
                index.jsp
                default.html
                default.htm
                default.jsp
            
            Archetype Created Web Application
            
                MyController
                controller.MyController
            
            
                MyController
                /MyController
            
    
    
    

    index.jsp

     <%@ page
            language="java"
            contentType="text/html; charset=UTF-8"
            pageEncoding="UTF-8"%>
        
        <%@ taglib
            uri="http://java.sun.com/jsp/jstl/core"
            prefix="c"%>
        
        
        
        Insert title here
        
        
    
    ${user.id} ${user.name}

    MyController.java

    package controller;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import domain.User;
    
    public class MyController extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        public MyController() {
            super();
        }
    
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
    
            RequestDispatcher rd = null;
            rd = request.getRequestDispatcher("index.jsp");
            rd.forward(request, response);
        }
    
        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            // Get name request parameter
            String name = request.getParameter("name");
    
            // User user1 = new User();
            // User user2 = new User();
            // user1.setId(0);
            // user1.setName("test1");
            // user2.setId(0);
            // user2.setName("test2");
    
            List users = new ArrayList();
    
            // users.add(user1);
            // users.add(user2);
    
    
    
             //DB Operation query your users
             users=query(name);
    
            request.setAttribute("users", users);
    
            RequestDispatcher rd = null;
            rd = request.getRequestDispatcher("index.jsp");
            rd.forward(request, response);
        }
    }
    

    so MyController#query method should be called from your service layer, and query according to your criteria(in demo name or can be anything)

    EDIT: Be sure user has get property methods, like getId and getName in this example.

    0 讨论(0)
提交回复
热议问题