0

这是打印到我的 jsp 页面的代码。但是我在页面中有其他代码。当我调用此函数时,我希望它在调用它的位置之后立即打印消息。我无法确定,因为我使用的是 xhtml 协商,但我怀疑它在 /html 标记之后打印。

这是我的功能

public Print(HttpServletRequest request,HttpServletResponse response){
        try{
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.print("<p>haha</p>");
        }catch(IOException e){
            e.printStackTrace();
        }
    }
};

这就是我所说的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Register</title>
</head>

<body>
<%@ page import="com.otrocol.app.*" %>
<%
    Print(request, response);
%>
</body>
</html>

这就是我认为的结果:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Register</title>
</head>

<body>
</body>
</html>
"haha"

这就是我想要的响应:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>Register</title>
    </head>  
    <body>
     "haha"
    </body>
    </html>

这是我得到的错误:

在此处输入图像描述

4

4 回答 4

3

JSP 使用它自己的 PrintWriter,即 JspWriter out。所以将它传递给(静态)函数。否则,您将使用第二个作家,并且缓冲一切都变得混乱。

此外,由于输出已经发生,请不要在函数中设置内容类型。

JSP 的顶部是一个不错的位置,也用于导入。

<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

当有一个作家时,该功能将打印在正文中的正确位置。

关于原因的直觉很好。顺便说一句,函数名称以小写字母开头。

于 2013-05-03T15:01:10.250 回答
1

这不是您问题的直接答案,但我相信您所做的事情只会给您带来痛苦,即使您让它发挥作用。您没有使用正确的工具来完成这项工作;JSP创建自定义 JSP 标记是从 Java 代码写入的更好选择。


代码示例:

注册.jsp

<%@ taglib prefix="custom" uri="/WEB-INF/custom-tags.tld" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Register</title>
</head>
<body>
    <p>
      <c:out value="${custom:printHaha()}" />
    </p>
</body>
</html>

自定义标签.tld

<?xml version="1.0" encoding="UTF-8"?>

<taglib version="2.0" 
        xmlns="http://java.sun.com/xml/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd">
    <tlibversion>1.0</tlibversion>
    <jspversion>2.0</jspversion>
    <shortname>custom-taglib</shortname>

    <uri>CustomTags</uri>

    <function>
        <name>printHaha</name>
        <function-class>com.yourpackage.Tags</function-class>
        <function-signature>
           java.lang.String print()
        </function-signature>
    </function>

    (...)

标签.class

public class Tags {
    public static String print() {
        return "haha";
    }
}

有关标签的更多信息:官方文档

于 2013-05-03T15:05:14.213 回答
0

我不检查您的代码...您不能在 jsp 页面中使用 get writer 再次执行 out.print ...因为已通过呈现 jsp 提交了此请求的响应

现在要在 asp 上打印一些东西,您可以通过多种方式执行此操作

  1. 按表达式标签打印
  1. 用尽(这是服务器创建的对象)

    out.print("废话...");

和更多

要了解 jsp 会发生什么,请查看 /work/catalina/blah.../

于 2013-05-03T14:52:19.670 回答
0

有两页。第一个是主页。这个执行一些伪计算。

根据这些计算,返回 Success.jsp 或 Failure.jsp。

这段代码将做你想要实现的......

尽管正如其他人指出的那样,最近有更高级的技术,但要跳舞,首先你必须知道动作......

一只忙碌的猫

主要看这个 cObj.Print(request, response); 在第二个jsp页面中。

JSP 页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>
<%@ page import="rustler.Beans.Beany" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>JSP and JavaBean</title>

        <%-- create an instance of Customer class --%>
        <jsp:useBean id="cObj" scope="request" class="rustler.Beans.Beany">

        <%-- Set the value of attribute such as CustID --%>
        <jsp:setProperty name="cObj" property="*" />
        </jsp:useBean>

    </head>
<body>

<%
            int x=cObj.setStoreCust();

            if(x>=1)
            {
        %>
                <jsp:forward page="Success.jsp" />
        <%

            }
            else
            {
        %>
                <jsp:forward page="Failure.jsp" />
        <%

            }

        %>

</body>
</html>

JSP 页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ page import="java.util.*" %>
<%@ page import="rustler.Beans.Beany" %>
<%@ page import="javax.servlet.http.HttpServletRequest" %>
<%@ page import="javax.servlet.http.HttpServletResponse" %>



<!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=ISO-8859-1">
<title>Failure!</title>

<%-- create an instance of Customer class --%>
<jsp:useBean id="cObj" scope="request" class="rustler.Beans.Beany">

<%-- Set the value of attribute such as CustID --%>
<jsp:setProperty name="cObj" property="*" />


</jsp:useBean>

</head>
<body>


<%cObj.Print(request, response);%>
</body>
</html>

爪哇豆

package rustler.Beans;

import java.io.*;
import java.util.*;
import java.sql.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Beany implements Serializable
{

    public Beany()
    {

    }
        /**
     * 
     */
    private static final long serialVersionUID = 1L;
        private String custID;
        private String custName;
        private int qty;
        private float price;
        private float total;
        private int storeCust;

        public String getCustID() {
            return custID;
        }

        public void setJunk(String sStr)
        {
            //System.out.println("What a punk!");
            custName = sStr;//"What a punk!";           
        }       

        public void Print(HttpServletRequest request,HttpServletResponse response)
        {
            try{
                response.setContentType("text/html");
                PrintWriter out = response.getWriter();
                out.print("<p>haha</p>");
            }catch(IOException e){
                e.printStackTrace();
            }
        }


        public String prntJunk()
        {
            //System.out.println("What a punk!");
            return custName;//"What a punk!";           
        }

        public void setCustID(String custID) {
            this.custID = custID;
        }

        public String getCustName() {
            return custName;
        }

        public void setCustName(String custName) {
            this.custName = custName;
        }

        public int getQty() {
            return qty;
        }

        public void setQty(int qty) {
            this.qty = qty;
        }

        public float getPrice() {
            return price;
        }

        public void setPrice(float price) {
            this.price = price;
        }

        public float getTotal() {
            return total;
        }

        public void setTotal(float total) {
            this.total = total;
        }

        public int setStoreCust() 
        {
            try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/usermaster","admin","password");
            PreparedStatement pstmt=null;
            String query=null;
            query="insert into customer values(?,?,?,?,?)";
            pstmt=con.prepareStatement(query);
            pstmt.setString(1,custID);
            pstmt.setString(2,custName);
            pstmt.setInt(3,qty);
            pstmt.setFloat(4,price);
            pstmt.setFloat(5,total);
            int i=pstmt.executeUpdate();

            this.storeCust=i;
            }
            catch(Exception e)
            {

            }
            return storeCust;
        }
}
于 2014-01-10T04:38:33.263 回答