環境:
- Ubuntu 12.04
- Apache Tomcat 7.0.27
- Eclipse 4.2(Juno)
- サーブレット3.0
- Openjdk 7
サーブレット3.0でFileCounterを実行しようとすると、URLでこのエラーが発生します
http:// localhost:8080 /wtp.filecounter/。
http:// localhost:8080 / wtp.filecounter / FileCounterである必要はありませんか?
Estado HTTP404-
Informedeestadoと入力します
mensaje
descripciónElrecursorequerido()noestádisponible。
Apache Tomcat / 7.0.26
構造
wtp.filecounter
dao
FileDao.java
servlets
FileCounter.java
FileDao.javaは、訪問者をファイルに読み取り、更新します
package wtp.filecounter.dao;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class FileDao {
public int getCount() {
int count = 0;
// Load the file with the counter
FileReader fileReader = null;
BufferedReader bufferedReader = null;
PrintWriter writer = null ;
try {
File f = new File("FileCounter.initial");
if (!f.exists()) {
f.createNewFile();
writer = new PrintWriter(new FileWriter(f));
writer.println(0);
}
if (writer !=null){
writer.close();
}
fileReader = new FileReader(f);
bufferedReader = new BufferedReader(fileReader);
String initial = bufferedReader.readLine();
count = Integer.parseInt(initial);
} catch (Exception ex) {
if (writer !=null){
writer.close();
}
}
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return count;
}
public void save(int count) throws Exception {
FileWriter fileWriter = null;
PrintWriter printWriter = null;
fileWriter = new FileWriter("FileCounter.initial");
printWriter = new PrintWriter(fileWriter);
printWriter.println(count);
// Make sure to close the file
if (printWriter != null) {
printWriter.close();
}
}
}
カウンタを読み取り、テキストプレーンで書き込むFileCounter.javaサーブレット
package wtp.filecounter.servlets;
import java.io.IOException;
import java.io.PrintWriter;
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.servlet.http.HttpSession;
import wtp.filecounter.dao.FileDao;
/**
* Servlet implementation class FileCounter
*/
@WebServlet("/FileCounter")
public class FileCounter extends HttpServlet {
private static final long serialVersionUID = 1L;
int count;
private FileDao dao;
public void init() throws ServletException {
dao = new FileDao();
try {
count = dao.getCount();
} catch (Exception e) {
getServletContext().log("An exception occurred in FileCounter", e);
throw new ServletException("An exception occurred in FileCounter"
+ e.getMessage());
}
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// Set a cookie for the user, so that the counter does not increate
// everytime the user press refresh
HttpSession session = request.getSession(true);
// Set the session valid for 5 secs
session.setMaxInactiveInterval(5);
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
if (session.isNew()) {
count++;
}
out.println("This site has been accessed " + count + " times.");
}
public void destroy() {
super.destroy();
try {
dao.save(count);
} catch (Exception e) {
e.printStackTrace();
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>wtp.filecounter</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
何が悪いのかについて何か考えはありますか?
サーバーで実行すると、プロジェクトは「http:// localhost:8080 / wtp.filecounter / FileCounter」ではなく「http:// localhost:8080 / wtp.filecounter /」を開き、2番目のURLが機能します。