@WebServlet(urlPatterns = "/") // javax.servlet-api 3.0
public class HomeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<String> users = Arrays.asList("Vaya", "Petya", "Fedya");
req.setAttribute("users", users); // с помощью атрибутов передаются данные между сервлетами
req.getRequestDispatcher("/home.jsp").forward(req,resp);
}
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<h2>Hello World!</h2>
<c:forEach items="${users}" var="usr">
<p>${usr}</p>
</c:forEach>
</body>
</html>
[
{name: "pNode 01", children: [
{name: "child 01"},
{name: "child 02"}
]}
]
java Game>file.txt
java -jar Game.jar>file.txt
static {
try {
System.setOut(new PrintStream(new File("file.txt")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Hello World!"); // будет записано в файл
}
Thread t = new Thread.... {
if (Thread.interrupted()) {
// сохранить текущий результат и выйти из метода
}
});;
t.start();
t.join(10000); // ждем поток 10 сек
t.interrupt(); // предлагаем потоку прервать выполнение
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
package ru.toster.java.q261000;
import java.io.*;
import java.util.*;
public class MainQ261000 {
public static void main(String[] args) {
String demoFile = "aaaaaaaaa\nbbbbbbbbbbbb\nccccccccccccccccc";
Reader r = new StringReader(demoFile);
// Reader r = new FileReader("FileWithTextLines.txt");
List<String> lines = new ArrayList<String>();
Scanner s = new Scanner(r);
try {
while(s.hasNextLine()) {
lines.add(s.nextLine());
}
} finally {
s.close();
}
for (String line : lines) {
System.out.println(line);
}
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mmocore</groupId>
<artifactId>authserver</artifactId>
<version>0.0.1</version>
<name>authserver</name>
</project>
<packaging>jar</packaging>
....
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>ru.toster.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</build>
...
mvn assembly:single
mvn package
<packaging>war</packaging>
File -> Export -> Runnable Jar File -> Package required libraries into generated JAR
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class App
{
public static void main( String[] args )
{
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("Hello");
}
}, new Date(), 100);
}
}
package ru.toster.java.q241826;
import java.util.Date;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
while (true) {
System.out.print("enter command>");
String command = scnr.nextLine();
if ("help".equals(command)) {
printListCommand();
} else if ("date".equals(command)) {
printDate();
} else if ("time".equals(command)) {
printTime();
} else if ("exit".equals(command)) {
System.out.println("Good Bye!");
break;
} else {
System.out.println("Unknown command! Please enter 'help'");
}
}
scnr.close();
}
private static void printTime() {
System.out.printf("%1tT\n", new Date());
}
private static void printDate() {
System.out.printf("%1tY-%1$tm-%1$td\n", new Date());
}
private static void printListCommand() {
System.out.println(
"'help'\tprint list commands;\n" +
"'exit'\texit from programm;\n" +
"'date'\tprint today's date;\n" +
"'time'\tprint current time;");
}
}