Exercise
3.1. Design and implement a test to verify that init()
is called only once.
3.2. Please run the thread sample code in Section 3.1, and observe the output.
3.3. In the mini-mvc
project, what if doPost()
method does not exist while the method="POST"
is specified in the form?
3.4. Which error would you encounter on the web page if you try to convert "3.14" to int
?
3.5. Extend mini-mvc
for book recommendation. Add a view to display:
Your age is xxx.
You name is xxx.
Recommendation:
1. XXX
2. XXX
Make sure the name can be in non-Latin languages, and it is able to output user-friendly message when the age is not valid.
3.6. In most cases, the browser' default validation using required
is not you want. Therefore, front-end frameworks can help you out. Try to understand form.html
in ch3/request
which is integrated with Bootstrap. (Skip this exercise if you don't know JavaScript)
3.7. Password confirmation validation is very common in sign up pages. Try to understand Password Validation. (Skip this exercise if you don't know JavaScript)
3.8. Can we use getParameterValues()
to obtain a single value parameter like "age" in ch3/request
? Design a test to verify your hypothesis.
3.9. Write a helper method to get the suffix of a file name for UploadServlet.java
in ch3/request
. For example, getSuffix("abc.png")
returns .png; getSuffix("123.abc.txt")
returns .txt.
String getSuffix(String path)
And then combine this method with UUID to save upload files.
3.9. Try to upload multiple files in a form.
3.10. We can customize many settings (e.g., the limiting size of the uploaded file) by @MultipartConfig
. For example, it could be constructed as follows:
@MultipartConfig(location="/tmp", fileSizeThreshold=1024*1024,
maxFileSize=1024*1024*5, maxRequestSize=1024*1024*5*5)
Please try to understand what this annotation means.
3.11. Bootstrap provides input group style for file uploading. Please add some code to upload2.html
in ch3/request
to let it upload files.
3.12. Add restrictions for upload.html
in ch3/request
to let it only allow .jpg, .jpeg and .png files. Note that the server-side checking is also required.
3.13. How does the payload look like in a multipart/form-data
request? Try to inspect it in the Network
tab.
3.14. Try to return a list of Book
objects as JSON string based on Json2Servlet.java
of ch3/response
.
3.15. Can you access cat.png
if it is put inside webapp/WEB-INF
? If not, please write code to return it as the response.
3.16. Please return a PDF file as the response.
3.17. In Section 3.5, we provided two methods to enable output stream to output the input stream. Particularly, the first method will get all bytes from the resource:
byte[] data = is.readAllBytes();
Clearly, it would result in much memory overhead if the resource is very large. A common trick is to read-write per fixed small length (e.g., 1024) of bytes. For example,
int read = 0;
byte[] bytes = new byte[1024];
while ((read = is.read(bytes)) != -1) {
os.write(bytes, 0, read);
}
Try to understand the code above and test in a servlet. (Note that the memory overhead is no longer a problem if we use Files.copy()
.)
3.18. For an HTTP response, we mainly care about its status code, content type and content. Both status code and content type belong to the response header, and you can view them in the Network
tab. Try to figure out what setHeader()
and addHeader()
are used for.
3.19. In Forward4Servlet.java
of ch3/response
. What is the wrong with the code? Can you have some conclusions?
3.20. By default, Tomcat would locate and display the index page (e.g., index.jsp
, index.html
) when we start a web application. In fact, we are also able to specify our customized index in DD. For example,
<welcome-file-list>
<welcome-file>home.html</welcome-file>
</welcome-file-list>
It means home.html
will be considered as the welcome page, and the content of <welcome-file>
accepts any valid resource name including a servlet's URL name.
3.21. Create a servlet which accepts a text and then generates a QRCode as the response. Please use Zxing
as the dependency.
Hint: MatrixToImageWriter
offers a static method writeToStream()
. You can find more at the API Doc.
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)