mercredi 24 octobre 2018

java's Model-View-Controller Design Pattern charset problems

I have a question about garbled character sets...I want to link to an HTML input information page through a JSP file.......Submit links to a servlet when entering Chinese information in HTML...Then use this servlet to connect to the database for addition, deletion, modification, and redirection to the JSP file of the former to display the Chinese information....But the JSP page displays garbled Chinese....Here's my code Any answer deserves a thank you We make progress together Link to the HTML code:

<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="/test/mVCstudnet">showAllMessage</a>
</body>
</html>

JSP display page function:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<%@ page import="test.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        List<Person> student = (List) request.getAttribute("student");
    %>
    <%
        if (student.isEmpty()) {
    %>
    <a href="/test/test.html">create</a>
    <%
        }
    %>
    <%
        for (Person person : student) {
            out.println("name:" + person.getName());
            out.print("<br>");
            out.println("id:" + person.getId());
            out.print("<br>");
            out.println("password:" + person.getPassword());
            out.print("<br>");
            out.println("money:" + person.getMoney());
            out.print("<br>");
    %>
    <a href="/test/delect?id=<%=person.getId()%>">delect</a>
    <a href="/test/update.jsp?id=<%=person.getId()%>">update</a>
    <%
        out.print("<br>");
        }
    %>
    <%
        if (student.size() >= 1) {
    %>
    <a href="/test/test.html">create</a>
    <%
        }
    %>  
</body>
</html>

HTML code:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/test/insert" method="post">
<br>
id<input type="text" name="id" value=""/>
<br>
name<input type="text" name="name" value=""/>
<br>
password<input type="text" name="password" value=""/>
<br>
money<input type="text" name="money" value=""/>
<br>
<input type="submit" value="submit"/>
</form>

</body>
</html>

Servlet code: Select the code for the functionality

package test;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/mVCstudnet")
public class MVCstudnet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          JDBCWay select=new JDBCWay();
          List <Person> student=select.select();
          String path="/student.jsp";
          request.setAttribute("student", student);
          request.getRequestDispatcher(path).forward(request, response);

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response); 
    }
}

Code that adds functionality:

package test;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/insert")
public class InsertStudent extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path="mVCstudnet";
        response.sendRedirect(path);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Insert  insert=new Insert();
        String id=request.getParameter("id");
        String name=request.getParameter("name");
        String password=request.getParameter("password");
        String money=request.getParameter("money");
        int _money=Integer.parseInt(money);
        insert.insert(id,name,password,_money);
        doGet(request, response);
    }

}

Connect to database insert function:

package test;

import java.sql.CallableStatement;
import java.sql.Connection;

public class Insert {
    Connection connection=null;

    public void  insert(String id,String name,String password,int money)
    {
        connection=JDBCTools.getConnection();
        try {
        CallableStatement callableStatement=connection.prepareCall("{call pro_insert(?,?,?,?)}");
        callableStatement.setString(1,id);
        callableStatement.setString(2, name);
        callableStatement.setString(3, password);
        callableStatement.setInt(4, money);
        callableStatement.executeUpdate();
        if (callableStatement != null) {
            callableStatement.close();
        }
        JDBCTools.closeConnection();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

}




Aucun commentaire:

Enregistrer un commentaire