[For myself] Browser display using JSP files and Servlet classes

Recorded because the connection between JSP and Servlet class was successful for the first time.

[Reference link] ・ About the display of the Html pull-down menu (https://murashun.jp/blog/20200128-66.html) ・ Html Create multi-line text input form (http://www.htmq.com/html/textarea.shtml)

[JSP file]

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Inquiry form</title>
</head>
<body>


<%--Ready to insert form--%>
<form action="/example/P157_s" method="post">

<%--Name input tab--%>
name<input type="text" name="name"><br>

<%--Inquiry type selection tab--%>
	<select name="qtype">
      <option value="company">About the company</option>
      <option value="product">About product</option>
      <option value="support">After-sales support</option>
      </select>

<%--Inquiry content input tab--%>
      <p>Please enter your inquiry</p>
      <textarea name="body" rows="4" cols="40">
      </textarea><br>

      <%--Submit button--%>
	<input type="submit" value="Send">

</form>
</body>
</html>

[Servlet class]

package p157_s;

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;

@WebServlet("/P157_s")
public class P157_s extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");

		String name=request.getParameter("name");
		String slags=request.getParameter("qtype");
		String comment=request.getParameter("body");

		//java code
		String errorM="";
		if(name==null||name.length()==0) {errorM+="No name has been entered";}
		if(slags==null||slags.length()==0) {errorM+="Inquiry type not selected";}
		else if(slags.equals("company")){slags="About the company";}
		else if(slags.equals("product")){slags="About the product";}
		else if(slags.equals("support")){slags="After-sales support";}
		if(comment==null||comment.length()==0) {errorM+="Your inquiry has not been entered";}

		String msg=("Full name["+name+"]Type of inquiry["+slags+"]Inquiry content["+comment+"]");
		if(errorM.length()!=0) {msg=errorM;}

	response.setContentType("text/html; charset=UTF-8");
		PrintWriter out=response.getWriter();

		out.println("<html>");
		out.println("<head>");
		out.println("<title>Inquiry form</title>");
		out.println("</head>");

		out.println("<meta.charSet=\"UTF-8\">");
		out.println("<body>");
		out.println("<p>"+msg+"</p>");
		out.println("</body>");
		out.println("</html>");
	}
}

Recommended Posts

[For myself] Browser display using JSP files and Servlet classes
Switched property files for development and production using JMockit
JSP error display from Servlet
A Simple CRUD Sample Using Java Servlet / JSP and MySQL
Classes and instances Java for beginners
Notes for reading and generating xlsx files from Java using Apache POI
[For myself] Transfer of Servlet class processing
Save files and folders using File Manager
Write code using Ruby classes and instances