I want to edit an jsp file from textarea in browser. So i need to upload jsp content to textarea.
For example i have a readMe.jsp file like that:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<h1>Read Me</h1>
<p> This is read me content</p>
I want this edit like that:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<h1>Read Me</h1>
<p> This is read me content</p>
<hr>
<h2>Read Me Second Head</h2>
<p>This is read me content 2</p>
The way I think to be able to do this in the browser is to upload the jsp content to textarea and edit it from there. Does anyone know how I can do this?
If I have a way to edit a jsp file differently from the browser, it can also be.
Thanks.
This is just an example how a JSP page can be edited from a browser.
webapp/readme.jsp is a page to edit.
<%# page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>Read Me</title>
</head>
<body>
<h1>Read Me</h1>
<p> This is read me content</p>
<p>Edit this page</p>
</body>
</html>
webapp/edit/readme.jsp contains a form within that a content of a readme.jsp page can be edit.
<%# page contentType="text/html;charset=UTF-8" %>
<html>
<head>
<title>Edit | Read Me</title>
</head>
<body>
<h1>'Read Me' edit page</h1>
<form method="post" action="${pageContext.request.contextPath}/edit/readme">
<label>
<textarea name="newContent" cols="96" rows="16">${content}</textarea>
</label>
<br><br>
<button type="submit">Save</button>
</form>
</body>
</html>
EditReadMe.java is a servlet.
The method doGet is triggered by the Edit this page link from the webapp/readme.jsp. It read content from the webapp/readme.jsp file and passes it to the webapp/edit/readme.jsp.
The method doPost is responsible for working with the form from the webapp/edit/readme.jsp and updating content for the webapp/readme.jsp file.
package com.example.editjsp;
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 java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
#WebServlet(name = "readMeEditor", value = "/edit/readme")
public class EditReadMe extends HttpServlet {
private static final String RESOURCE_PATH = "/readme.jsp";
private Path pathToFile;
#Override
public void init() {
pathToFile = Path.of(getServletContext().getRealPath(RESOURCE_PATH));
}
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String contentToEdit = Files.readString(pathToFile);
req.setAttribute("content", contentToEdit);
req.getRequestDispatcher("/edit/readme.jsp").forward(req, resp);
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Files.writeString(pathToFile, req.getParameter("newContent"));
resp.sendRedirect(req.getContextPath() + RESOURCE_PATH);
}
}
Related
What I am trying to do is a blog page with jsp. For this purpose, I save the content of blogs in an html file. I am using a jsp file to update the content in the existing html file via the browser. I used the textarea tag in a jsp file to edit this html file through the browser. There is no encoding problem in the html content transferred to the textarea. But when I make changes to the html file and save it, the utf-8 characters turn out to be incorrect. For Example:
The html file is read and placed comfortably in the textarea without any encoding problems
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Evrim Üzerine Notlar</h1>
</body>
</html>
When i submit for save it:
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Evrim Ãzerine Notlar</h1>
</body>
</html>
There is the classes and jsp files:
setBlog.jsp (Html file is setting in this file)
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<jsp:include page="navbar.jsp"/>
<form action="adminBlogProcess?meth=update" method="post">
id:<input type="text" readonly="readonly" value="${setMe.textId}" name="tid">
<label>Text Title</label>
<input type="text" name="name" class="form-control" value="${setMe.name}"/>
<label>File Path</label>
<input type="text" name="textPath" class="form-control" value="${setMe.textPath}"/>
<label>Kategori</label>
<select class="form-control" name="selectedCategory">
<c:forEach items="${loc}" var="val">
<option value="${val.categoryId}">${val.categoryName}</option>
</c:forEach>
</select>
<label>İçerik:</label>
<textarea class="form-control" name="content" rows="100">${contentOfDoc}</textarea>
<input type="submit" class="btn btn-primary" value="Save Changes">
</form>
</body>
</html>
AdminBlogProcess.java
package Controller;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
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 DataAccess.CategoryDao;
import DataAccess.TextDao;
import Model.Category;
import Model.Text;
#WebServlet("/adminBlogProcess")
public class AdminBlogProcess extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -7693704779591981580L;
private CategoryDao cd;
private TextDao td;
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String meth = req.getParameter("meth");
String msg="";
if (meth.equalsIgnoreCase("update")) {
update(req);
msg="Yazı Güncellendi!";
} else if (meth.equalsIgnoreCase("add")) {
} else if (meth.equalsIgnoreCase("delete")) {
}
req.setAttribute("message", msg);
req.getRequestDispatcher("/admin/admin.jsp").forward(req, resp);
}
//This update method is write new content into the html file
private void update(HttpServletRequest req) {
String name=req.getParameter("name");
String textPath=req.getParameter("textPath");
int id=Integer.valueOf(req.getParameter("tid"));
Category ct=getCd().getCategoryById(Integer.valueOf(req.getParameter("selectedCategory")));
Text txt=new Text(name, textPath, ct, id);
getTd().update(txt);
String content=req.getParameter("content");
String path="C:\\Users\\90551\\Desktop\\Eclipse Projeler\\2022 Yaz-Haziran Arsiv\\JAVA SERVER PAGES\\004BlogSitesi\\src\\main\\webapp\\texts\\"+textPath;
try (FileWriter fw = new FileWriter(new File(path), StandardCharsets.UTF_8);
BufferedWriter bw = new BufferedWriter(fw)){
bw.write("");
bw.append(content);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public CategoryDao getCd() {
if (cd == null) {
cd = new CategoryDao();
}
return cd;
}
public void setCd(CategoryDao cd) {
this.cd = cd;
}
public TextDao getTd() {
if (td == null) {
td = new TextDao();
}
return td;
}
public void setTd(TextDao td) {
this.td = td;
}
}
What should i do for fix that encodin problem?
HTML code appears when I try to execute the jsp page. This appears in the browser.
I tried to change the contentType and language in the tag. But it did not work.
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<HTML>
<HEAD> <TITLE> The Welcome JSP </TITLE> </HEAD>
<BODY>
<H3> Welcome! </H3>
<P><B> Today is <%= new java.util.Date() %>. Have a nice day! </B></P>
</BODY>
</HTML>
This is my servlet code.
public class Add extends HttpServlet {
public void service(HttpServletRequest rq, HttpServletResponse rs)throws IOException, ServletException
{
int i=Integer.parseInt(rq.getParameter("t1"));
int j=Integer.parseInt(rq.getParameter("t2"));
int k=i+j;
PrintWriter out=rs.getWriter();
out.println("The sum is:"+k);
RequestDispatcher rd=rq.getRequestDispatcher("/Welcome.jsp");
rd.include(rq,rs);
}
}
I want the code to get rendered and view the output.
I have to ask you because i'm stuck and can't go on. I'm trying to make response path like this:
DAO-> Servlet-> JSP
The process is: user fills the form on jsp, clicks "Submit" and gets response on the same JSP (user added or didn't)
adding data to DB is working fine but i got error from WebBrowser :
Type Exception Report
Message can't parse argument number: pageContext.request.contextPath
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
here is my jsp code:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix ="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix ="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%# taglib prefix ="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%# page isELIgnored="false"%>
<script type="text/javascript" src="${pageContext.request.contextPath}/jQuery.js"/></script>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>dodaj pracownika</title>
</head>
<body>
<f:view>
<section id = registration" class = "section">
<div class "container tagline">
<em>{0}</em>
</div>
<br>
<AdContentProvider
Name="MIME-type"
Provider="addUser.class"
Properties="optional-properties-for-your-class"
>
</AdContentProvider>
<form action="${pageContext.request.contextPath}/newuser" method = "post">
<form>
<label> Imie</label> <input type="text" name= "imie" id="imie"> <br/>
<label> Nazwisko</label> <input type="text" name= "nazwisko" id="nazwisko"> <br/>
<label> Stanowisko</label> <input type="text" name= "stanowisko" id="stanowisko"> <br/>
<label> Stawka</label> <input type="text" name= "stawka" id="stawka"> <br/>
<input type ="submit" value="Dodaj" id = "dodaj">
</form>
</f:view>
</body>
</html>
and servlet code:
package tk.jewsbar.servlets;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.MessageFormat;
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.transaction.Transactional;
import fasolki.Pracownik;
import tk.jewsbar.dao.Dao;
#WebServlet("/newuser")
public class addUser extends HttpServlet
{
#Override
#Transactional
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
String imie = req.getParameter("imie");
String nazwisko = req.getParameter("nazwisko");
String stanowisko = req.getParameter("stanowisko");
double stawka = Double.valueOf(req.getParameter("stawka"));
Pracownik pracownik = new Pracownik (imie, nazwisko, stanowisko, stawka);
Dao dao = new Dao();
int rows = dao.dodajUzytkownika(pracownik);
String err = "null";
if (rows ==0)
{
err = "Niestety nie udalo sie dodac uzytkownika";
}
else {
err = "Dodano uzytkownika" + rows;
}
String pejcz = getHTMLString(req.getServletContext().getRealPath("html/newuser.jsp"), err);
resp.getWriter().write(pejcz);
}
public String getHTMLString(String sciezka, String message) throws IOException
{
BufferedReader czytaj = new BufferedReader (new FileReader(sciezka));
String linia = "";
StringBuffer bufor = new StringBuffer();
while((linia=czytaj.readLine())!= null)
{
bufor.append(linia);
}
czytaj.close();
String pejcz = bufor.toString();
pejcz = MessageFormat.format(pejcz, message);
return pejcz;
}
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String pejcz = getHTMLString(req.getServletContext().getRealPath("html/newuser.jsp"),"");
resp.getWriter().write(pejcz);
}
}
try like this
<script type="text/javascript" src="<c:out value="${contextPath}"/>/jQuery.js"></script>
or
<script type="text/javascript" src="<c:out value="${pageContext.request.contextPath}"/>/jQuery.js"></script>
Instead of
<script type="text/javascript" src="${contextPath}/jQuery.js"/></script>
You may read the suggestions on this link ,
expression-language-in-jsp-not-working
I am really confused about this question. I have tried everything and nothing works.
I want to be able to get the volume and display it as param.volume. However it will return zero if I do it as a scriptlet.
I want to be able to create the getter method named getVolume that returns the volume of the cube when called (side * side * side).
My getter method is:
package servlets;
public class Cube {
private int side;
private int volume;
public void setVolume(int volume){
this.volume=volume;
}
public int getVolume() {
volume=side*side*side;
return volume;
}
public int getSide() {
return side;
}
public void setSide(int side) {
this.side = side;
}
}
In the CalcServlet:
package servlets;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CalcServlet
*/
#WebServlet("/calc")
public class CalcServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String scube=request.getParameter("cube");
Cube obj=new Cube();
try{
int cube=Integer.parseInt(scube);
obj.setSide(cube);
obj.getVolume();
request.setAttribute("cube", obj);
}catch(NumberFormatException e){
}
ServletContext sc = this.getServletContext();
//RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/pages/result.jsp");
RequestDispatcher rd = sc.getRequestDispatcher ("/WEB-INF/pages/result.jsp");
rd.forward(request, response);
}
}
In result.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib tagdir="/WEB-INF/tags" prefix="myjsp" %>
<%# page import="servlets.Cube, servlets.CalcServlet " %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<body>
<h2>Cube Info Using JSP 2.0 </h2><br>
<b>Side of a Cube: ${param.side} </b><br>
<b>Volume of a Cube: ${param.side*param.side*param.side}</b><br>
<br></br><br>
Using Custom Tag:
<myjsp:displayform color="red"></myjsp:displayform>
<br></br><br>
<h2>Using JSTL:</h2>
<h2>Cube Info Using JSP 2.0 </h2><br>
<b>Side of a Cube: ${param.side} </b><br>
<b>Volume of a Cube: </b>
<c:choose>
<c:when test="${param.side*param.side*param.side>10}">
<li><span style="color: red;">Volume: ${param.side*param.side*param.side}</span></li>
</c:when>
<c:otherwise>
<li><span style="color: black;">Volume: ${param.side*param.side*param.side}</span></li>
</c:otherwise>
</c:choose>
</body>
</html>
The result displays because param.side is being read. However I want to do this with param.volume. Please help!! I have tried everything.
first fix your getter/setter for volume. right now your getter act like a setter.
public void setVolume(int Volume){
this.volume=volume;
}
public int getVolume() {
return volume;
}
then in your serlvet (proper pratice would be to do that in an utility class)
int side=Integer.parseInt(scube);
obj.setSide(side);
int volume= side*side*side;
obj.setVolume(volume);
request.setAttribute("cube", obj);
you named your parameter which contains the bean Cube "cube". On that line
request.setAttribute("cube", obj);
so to access the bean cube in your JSP you should do like that
${cube.side}
${cube.volume}
or the proper way
<c:out value="${cube.side}"/>
<c:out value="${cube.volume}"/>
For anyone who wants to know the answer, I got it from a friend. Hope this will benefit the community.
In result.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib tagdir="/WEB-INF/tags" prefix="myjsp" %>
<%# page import="servlets.Cube"%>
<%
Cube cube = (Cube) request.getAttribute("cube");
int side = 0, volume = 0;
if (cube != null) {
side = cube.getSide();
volume = cube.getvolume();
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<body>
<h2>Cube Info Using JSP 2.0 </h2><br>
<b>Side of a Cube: ${cube.side} </b><br>
<b>Volume of a Cube: ${cube.getvolume()}</b><br>
<br></br><br>
Using Custom Tag:
<myjsp:displayform color="red"></myjsp:displayform>
<br></br><br>
<h2>Using JSTL:</h2>
<h2>Cube Info Using JSP 2.0 </h2><br>
<b>Side of a Cube: ${cube.side} </b><br>
<b>Volume of a Cube: <c:if test="${cube.volume}">10><font color="red"></font>the volume is ${cube.getvolume()}</c:if></b><br>
</body>
</html>
And in calcservlet:
package servlets;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class CalcServlet
*/
#WebServlet("/calc")
public class CalcServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/pages/result.jsp");
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String scube=request.getParameter("side");
Cube obj=new Cube();
try{
int cube=Integer.parseInt(scube);
obj.setSide(cube);
System.out.println("works");
obj.getvolume();
request.setAttribute("cube", obj);
}catch(NumberFormatException e){
}
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/pages/result.jsp");
rd.forward(request, response);
}
}
In Cube:
package servlets;
public class Cube {
private int side;
public int getvolume() {
return side*side*side;
}
public int getSide() {
return side;
}
public void setSide(int side) {
this.side = side;
}
}
In form.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Form</title>
</head>
<body>
</head>
<body>
<h2>Side Form</h2> <!-- page heading -->
<form method=post action="calc"><!-- open a neew form -->
Side of a Cube: <input type=number name=side required><br> <!-- the iname parameter -->
<br></br>
<input type=submit value=Calculate><br> <!-- submit to the home page if everything is fine, else additem servlet and display the new item -->
<br><br><br></br>
</form> <!-- close form -->
</body>
</html>
I want to create a simple JSP page. I have an EJB, in this there is a session bean.
I have a JSP page and a Servlet, but I have a strange situation.
When I click execute on my page, this turns in a white page and don't give me the result.
I post here my code, can you help me please.
Servlet:
package web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.*;
import javax.servlet.http.*;
import ejb.calc;
/**
* Servlet implementation class calcServlet
*/
public class calcServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public calcServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession(true);
RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/index.jsp");
float a=Float.parseFloat(request.getParameter("n1"));
float b=Float.parseFloat(request.getParameter("n2"));
char oper=request.getParameter("oper").charAt(0);
float result=0;
try {
Context ctx=new InitialContext();
// call the calcImpl class of the SimpleCalculator EJB with the mappedName
calc cl=(calc) ctx.lookup("Firstcalc");
switch(oper){
case '+': result=cl.sum(a, b); break;
case '-': result =cl.minus(a, b); break;
case '*': result =cl.mult(a, b); break;
case '/': result =cl.div(a, b); break;
}
session.setAttribute("result",result);
request.setAttribute("a", a);
request.setAttribute("b", b);
}
catch(NamingException e)
{session.setAttribute("erreur: ",e.getMessage());
}rd.forward(request,response);
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
JSP:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2> <b> Hello World To The Simple Calculator </b> </h2> <% float a=2,b=1; if (request.getAttribute("a")!=null) a=Float.parseFloat(request.getAttribute("a").toString()); if( request.getAttribute("b")!=null) b=Float.parseFloat(request.getAttribute("b").toString()); %> <form method="post" action="calcServlet"> <b>Number 1:</b><input type='text' name='n1' value="<%=a%>" /> <br/>
<b>Number 2:</b><input type='text' name='n2' value="<%=b%>" /> <br/>
<u><b>Options:</b></u> <br/>
<ul>
<li><b>+</b><input type='radio' name="oper" value='+' checked /></li>
<li><b> -</b><input type='radio' name="oper" value='-' /></li>
<li><b>*</b><input type='radio' name="oper" value='*' /></li>
<li> <b>/</b><input type='radio' name="oper" value='/' /></li> </ul>
<b>-------------------------------------------</b> <br/>
<input type="submit" value="Executer" /> </form>
<font color='blue'><b>Result is: </b> <%=session.getAttribute("result")%> </font> <br/> <font color='red' >Error: <%=session.getAttribute("error")%></font>
</body>
</html>
A JSP will blank out when you use old fashioned scriptlets (those <% %> things) and one of such a scriptlet has thrown an exception while the response has already been committed. It's too late then to display the error page. The browser ends up with a halfbaked HTML page (the HTML generated by JSP is incomplete and the browser will usually go blank). You should read the server logs for the exception and fix the code accordingly.
Unrelated to the actual problem, your approach is pretty clumsy. You don't need scriptlets at all. Just use EL (those ${} things). It has instant access to request parameters. E.g.
<input type="text" name="n1" value="${param.n1}" />
(for extra course points: use JSTL fn:escapeXml() to prevent XSS)
You don't even need to duplicate them as request attributes in the servlet. You should also not store the result as session attribute (it would be shared among all browser windows/tabs in the same session, you don't want to have this for a request based variable). Store it as request attribute
request.setAttribute("result", result);
and access it by EL as follows, it has instant access to page/request/session/application scoped attributes by just its name:
<b>Result is: </b> ${result}
Related questions:
Simple calculator in JSP/Servlet (with optional Ajax)
How to avoid Java code in JSP files