java的模型-视图-控制器设计模式字符集问题

时间:2018-10-24 11:26:13

标签: java web

我有一个关于乱码字符集的问题...我想通过JSP文件链接到HTML输入信息页面....在HTML中输入中文信息时提交到servlet的链接...然后使用该servlet连接到数据库以进行添加,删除,修改和重定向到前者的JSP文件以显示中文信息。...但是JSP页面显示乱码中文。...任何答案都值得感谢.... 我们共同进步... 这是我的代码

链接到HTML代码:

<%@ 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显示页面功能:

<%@ 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代码:

<!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代码: 选择功能代码

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

添加功能的代码:

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

}

连接到数据库插入功能:

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

}

0 个答案:

没有答案