我不得不问你,因为我被卡住了,不能继续下去。我试图做这样的响应路径:

DAO-> Servlet-> JSP

过程是:用户在jsp上填写表单,单击“提交”并在同一JSP上获得响应(用户已添加或未添加)

将数据添加到数据库工作正常,但我从WebBrowser收到错误消息:

类型异常报告

邮件无法解析参数编号:pageContext.request.contextPath

描述服务器遇到意外情况,阻止其满足请求。

这是我的jsp代码:



<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix ="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix ="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix ="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<%@ page isELIgnored="false"%>
<script type="text/javascript" src="${pageContext.request.contextPath}/jQuery.js"/></script>

<!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>dodaj pracownika</title>
</head>
<body>
<f:view>

<section id = registration" class = "section">
<div class "container tagline">
<em>{0}</em>


</div>
<br>

<AdContentProvider
Name="MIME-type"
Provider="addUser.class"
Properties="optional-properties-for-your-class"
>
</AdContentProvider>

<form action="${pageContext.request.contextPath}/newuser" method = "post">

<form>
<label> Imie</label> <input type="text" name= "imie" id="imie"> <br/>
<label> Nazwisko</label> <input type="text" name= "nazwisko" id="nazwisko"> <br/>
<label> Stanowisko</label> <input type="text" name= "stanowisko" id="stanowisko"> <br/>
<label> Stawka</label> <input type="text" name= "stawka" id="stawka"> <br/>

<input type ="submit" value="Dodaj" id = "dodaj">

</form>







</f:view>
</body>
</html>





和Servlet代码:

package tk.jewsbar.servlets;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.MessageFormat;

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

import fasolki.Pracownik;
import tk.jewsbar.dao.Dao;



@WebServlet("/newuser")
public class addUser extends HttpServlet
{

@Override
@Transactional
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{


    String imie = req.getParameter("imie");
    String nazwisko = req.getParameter("nazwisko");
    String stanowisko = req.getParameter("stanowisko");
    double stawka = Double.valueOf(req.getParameter("stawka"));




    Pracownik pracownik = new Pracownik (imie,  nazwisko,  stanowisko, stawka);
    Dao dao = new Dao();


    int rows = dao.dodajUzytkownika(pracownik);







    String err = "null";


if (rows ==0)
{
    err = "Niestety nie udalo sie dodac uzytkownika";
}

else {

    err = "Dodano uzytkownika" + rows;
}

String pejcz = getHTMLString(req.getServletContext().getRealPath("html/newuser.jsp"), err);
resp.getWriter().write(pejcz);


}

public String getHTMLString(String sciezka, String message) throws IOException
{

    BufferedReader czytaj = new BufferedReader (new FileReader(sciezka));

    String linia = "";

    StringBuffer bufor = new StringBuffer();

    while((linia=czytaj.readLine())!= null)

            {
        bufor.append(linia);

            }

    czytaj.close();

        String pejcz = bufor.toString();

        pejcz = MessageFormat.format(pejcz,  message);


        return pejcz;

            }




    @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

            String pejcz = getHTMLString(req.getServletContext().getRealPath("html/newuser.jsp"),"");
            resp.getWriter().write(pejcz);
        }

}

最佳答案

这样尝试

<script type="text/javascript"  src="<c:out value="${contextPath}"/>/jQuery.js"></script>


要么

<script type="text/javascript"  src="<c:out value="${pageContext.request.contextPath}"/>/jQuery.js"></script>


代替

<script type="text/javascript" src="${contextPath}/jQuery.js"/></script>


您可以阅读此链接上的建议,
expression-language-in-jsp-not-working

关于java - 在jsp上创建/显示servlet响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52184324/

10-17 03:11