sql >> Databáze >  >> RDS >> Mysql

hledat kód v servletu na mysql?

Vytvářím jednoduchý demo projekt, o kterém se zmiňujete, používám jstl:core knihovnu, kterou lze stáhnout z zde

web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
        id="WebApp_ID"
        version="3.1">
        <display-name>TestServlet</display-name>
        <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>
        <display-name>Archetype Created Web Application</display-name>
        <servlet>
            <servlet-name>MyController</servlet-name>
            <servlet-class>controller.MyController</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>MyController</servlet-name>
            <url-pattern>/MyController</url-pattern>
        </servlet-mapping>

</web-app>

index.jsp

 <%@ page
        language="java"
        contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%@ taglib
        uri="http://java.sun.com/jsp/jstl/core"
        prefix="c"%>
    <html>
    <head>
    <meta
        http-equiv="Content-Type"
        content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
<form
        action="MyController"
        method="post">
        <input
            type="text"
            name="name"
            value="yourUserName"> <input
            type="submit"
            value="Search">
    </form>
        <table title="Users List">
            <c:forEach
                items="${users}"
                var="user">
                <tr>
                    <td>${user.id}</td>
                    <td>${user.name}</td>
                </tr>
            </c:forEach>
        </table>
    </body>
    </html>

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<User> users = new ArrayList<User>();

        // 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);
    }
}

takže MyController#query metoda by měla být volána z vaší servisní vrstvy a dotazovat se podle vašich kritérií (v demo názvu nebo může být cokoliv)

UPRAVIT: Ujistěte se, že user má metody vlastnosti get, jako je getId a getName v tomto příkladu.




  1. Jak Cot() funguje v PostgreSQL

  2. Transparentní šifrování dat a vždy šifrováno

  3. MYSQL last_insert_id() a souběžnost

  4. Createuser:nemohl se připojit k databázi postgres:FATAL:role tom neexistuje