JSP Web应用开发
网站栏输入地址可以正常打开的JSP网页为什么用IDEA的浏览器打不开?
猜测:
IDEA浏览器打开的URL :file:///D:/JSPproject/ch10/web/10-1.jsp
自己输入的URL :http://localhost:8080/ch10/10-1.jsp
应该是这里的问题但不知道怎么解释。
源代码文章最后已给出
用IDEA的浏览器打开
出现的结果(能解释一下原因吗?)
在地址栏里输入localhost:/ch10/10-1.jsp,则可以正常出现
点击查询出现的结果也正常
XML 文件 score.xml
<?xml version="1.0" encoding="utf8"?>
<studentscore>
<studentnumber id="001">
<name>李明</name>
<grade>85</grade>
</studentnumber>
<studentnumber id="002">
<name>王五</name>
<grade>67</grade>
</studentnumber>
<studentnumber id="003">
<name>钱二</name>
<grade>95</grade>
</studentnumber>
</studentscore>
JSP文件 10-1.jsp
<%--
Created by IntelliJ IDEA.
User: 26316
Date: 2023/3/20
Time: 16:03
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="servletDom" method="get">
<input type="submit" value="查询所有学生成绩">
</form>
</body>
</html>
JavaBean 文件 ScoreBean.java
package com.jqe.example10_1;
public class ScoreBean {
private String id;
private String name;
private String grade;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
}
Servlet文件 ServletDom.java
package com.jqe.example10_1;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ServletDom extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
PrintWriter out = resp.getWriter();
try {
ArrayList<ScoreBean> domparse = domParse(new File("e:/score.xml"));
for(ScoreBean score:domparse){
out.println("姓名:"+score.getName()+",");
out.println("成绩:"+score.getGrade());
out.println("");
}
}catch (ParserConfigurationException e){
e.printStackTrace();out.println("1");
}catch (SAXException e){
e.printStackTrace();out.println("2");
}
}
public ArrayList<ScoreBean> domParse(File file) throws ParserConfigurationException,SAXException,IOException{
DocumentBuilderFactory factory = DocumentBuilderFactory.newNSInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file);
Element element = document.getDocumentElement();
NodeList nodeList = element.getElementsByTagName("studentnumber");
ArrayList<ScoreBean> list = new ArrayList<ScoreBean>();
for(int i=0;i<nodeList.getLength();i++){
ScoreBean score = new ScoreBean();
Element item = (Element) nodeList.item(i);
String id = item.getAttribute("id");
score.setId(id);
NodeList childNodes = item.getChildNodes();
for (int j = 0;j<childNodes.getLength();j++){
Node item2 = childNodes.item(j);
if (item2.getNodeType()==Node.ELEMENT_NODE){
if(item2.getNodeName().equals("name"))
score.setName(item2.getFirstChild().getNodeValue());
else if (item2.getNodeName().equals("grade"))
score.setGrade(item2.getFirstChild().getNodeValue());
}
}
list.add(score);
}
return list;
}
}
部署文件 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>servletDom</servlet-name>
<servlet-class>com.jqe.example10_1.ServletDom</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servletDom</servlet-name>
<url-pattern>/servletDom</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ServletSax</servlet-name>
<servlet-class>com.jqe.example10_2.ServletSax</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletSax</servlet-name>
<url-pattern>/ServletSax</url-pattern>
</servlet-mapping>
</web-app>